test/java/awt/print/PaintSetEnabledDeadlock/PaintSetEnabledDeadlock.java

Print this page




  37 
  38 public class PaintSetEnabledDeadlock extends Frame {
  39 
  40     final TestPanel panel;
  41     final Button button;
  42 
  43     public static void main(String[] args) {
  44         PaintSetEnabledDeadlock frame = new PaintSetEnabledDeadlock();
  45         frame.setSize(200, 200);
  46         frame.setVisible(true);
  47 
  48         Robot robot = Util.createRobot();
  49         robot.setAutoDelay(100);
  50         robot.setAutoWaitForIdle(true);
  51 
  52         for (int i = 0; i < 20; ++i) {
  53             Util.clickOnComp(frame.panel, robot);
  54             Util.clickOnComp(frame.button, robot);
  55         }
  56 
  57         frame.panel.stop();
  58         frame.dispose();
  59 


  60         System.out.println("Test passed.");
  61     }
  62 
  63     public PaintSetEnabledDeadlock() {
  64         super("7108598 test");
  65         setLayout(new GridLayout(1, 2));
  66         addWindowListener(new WindowAdapter() {
  67 
  68             @Override
  69             public void windowClosing(WindowEvent e) {
  70                 panel.stop();
  71                 System.exit(0);
  72             }
  73         });
  74         panel = new TestPanel();
  75         add(panel);
  76         button = new Button("Enable");
  77         button.addMouseListener(new MouseAdapter() {
  78 
  79             @Override


 123                 paramGraphics.drawImage(image, 0, 0, this);
 124             }
 125         }
 126     }
 127 
 128     @Override
 129     public void run() {
 130         while (active) {
 131             try {
 132                 synchronized (sync) {
 133                     sync.wait();
 134                 }
 135             } catch (InterruptedException ex) {
 136             }
 137             if (active) {
 138                 draw();
 139             }
 140         }
 141     }
 142 
 143     public void stop() {
 144         active = false;
 145         try {
 146             synchronized (sync) {
 147                 sync.notify();
 148             }
 149             synchronized (thread) {
 150                 thread.wait();
 151             }
 152         } catch (InterruptedException ex) {

 153         }

 154     }
 155 
 156     public void draw() {
 157         synchronized (getTreeLock()) {
 158             if (image != null) {
 159                 Graphics localGraphics = image.getGraphics();
 160                 Dimension size = getSize();
 161                 localGraphics.setColor(isEnabled() ? Color.green : Color.red);
 162                 localGraphics.fillRect(0, 0, size.width, size.height);
 163                 super.paint(localGraphics);
 164                 localGraphics.dispose();
 165                 getTreeLock().notifyAll();
 166             }
 167         }
 168     }
 169 
 170     public void sync() {
 171         synchronized (sync) {
 172             sync.notify();
 173         }


  37 
  38 public class PaintSetEnabledDeadlock extends Frame {
  39 
  40     final TestPanel panel;
  41     final Button button;
  42 
  43     public static void main(String[] args) {
  44         PaintSetEnabledDeadlock frame = new PaintSetEnabledDeadlock();
  45         frame.setSize(200, 200);
  46         frame.setVisible(true);
  47 
  48         Robot robot = Util.createRobot();
  49         robot.setAutoDelay(100);
  50         robot.setAutoWaitForIdle(true);
  51 
  52         for (int i = 0; i < 20; ++i) {
  53             Util.clickOnComp(frame.panel, robot);
  54             Util.clickOnComp(frame.button, robot);
  55         }
  56 
  57         boolean ret = frame.panel.stop();
  58         frame.dispose();
  59 
  60         if(!ret)
  61             throw new RuntimeException("Test failed!");
  62         System.out.println("Test passed.");
  63     }
  64 
  65     public PaintSetEnabledDeadlock() {
  66         super("7108598 test");
  67         setLayout(new GridLayout(1, 2));
  68         addWindowListener(new WindowAdapter() {
  69 
  70             @Override
  71             public void windowClosing(WindowEvent e) {
  72                 panel.stop();
  73                 System.exit(0);
  74             }
  75         });
  76         panel = new TestPanel();
  77         add(panel);
  78         button = new Button("Enable");
  79         button.addMouseListener(new MouseAdapter() {
  80 
  81             @Override


 125                 paramGraphics.drawImage(image, 0, 0, this);
 126             }
 127         }
 128     }
 129 
 130     @Override
 131     public void run() {
 132         while (active) {
 133             try {
 134                 synchronized (sync) {
 135                     sync.wait();
 136                 }
 137             } catch (InterruptedException ex) {
 138             }
 139             if (active) {
 140                 draw();
 141             }
 142         }
 143     }
 144 
 145     public boolean stop() {
 146         active = false;
 147         try {
 148             sync();
 149             thread.join(1000);
 150             if (thread.isAlive()) {
 151                 thread.interrupt();
 152                 return false;
 153             }
 154         } catch (InterruptedException ex) {
 155             return false;
 156         }
 157         return true;
 158     }
 159 
 160     public void draw() {
 161         synchronized (getTreeLock()) {
 162             if (image != null) {
 163                 Graphics localGraphics = image.getGraphics();
 164                 Dimension size = getSize();
 165                 localGraphics.setColor(isEnabled() ? Color.green : Color.red);
 166                 localGraphics.fillRect(0, 0, size.width, size.height);
 167                 super.paint(localGraphics);
 168                 localGraphics.dispose();
 169                 getTreeLock().notifyAll();
 170             }
 171         }
 172     }
 173 
 174     public void sync() {
 175         synchronized (sync) {
 176             sync.notify();
 177         }