Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  One Test Application for synchronized() wait()-notify() blocks  (Read 1327 times)
0 Members and 1 Guest are viewing this topic.
Offline broumbroum

Junior Member





« Posted 2007-09-26 23:52:31 »

So far, there's no garranty that a blocked Thread (such in Thread.State BLOCKED) would come to resume its state to RUNNING, but the waiting Thread (in Thread.WAITING) will surely resume on notify(). In fact  if notify() and wait() are used with care, you get the correct Thread wait and continue behaviour. Without it, that would make no sense to synchronize, as by the definition it can be incomplete if left unmanaged... Roll Eyes So to "take a tour" in this complex stuff for beginners, I've made a simple application by myself to illustrate better what Synchronization IS and ISN'T. compile with : shell-cmd$ javac MainLockT.java and run : shell-cmd$ java MainLockT

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MainLockT {
static String statusThreadLock(Object lock, Thread t) {
        String s = (t.holdsLock(lock))?t.getName() + " holds lock on lock " + lock.toString():t.getName() + " doesn't hold lock on lock " + lock.toString();
   System.out.println(s);  
   text.append(s+ "\r\n");  
   sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMaximum());
   text.repaint();
   return s;
}

static Thread getNotifier() {
   Thread t2 = new Thread(new Runnable() { public void run() {
                   statusThreadLock(lock, Thread.currentThread());
      try {
            synchronized(lock) {                  
           JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread()));        
         final long timer = System.currentTimeMillis();
         JFrame f = new JFrame();
         JComponent c;
         f.getContentPane().add(c = new JComponent() { public void paint(Graphics g) {
            super.paint(g);
            double rest = 5.0 - (double)(System.currentTimeMillis() - timer) / 1000.0;
            g.drawString((int)rest + " sec", 10, (int)((double)(this.getHeight() - g.getFontMetrics().getHeight()) / 2.0));
         }});
         c.setPreferredSize(new Dimension(100, 50));
         f.pack();
         f.setLocationRelativeTo(text);
         long wait = 0;
         long i = (long)(1000.0 / 24.0);
               while(5000 > (wait += i)) {
            f.setVisible(true);
            Thread.sleep(i);
            f.repaint();
         }
                   lock.notify();
         f.setVisible(false);
         f.dispose();
            }      
       } catch(InterruptedException e) { e.printStackTrace(); }

   }}, "5sec-NOTIFIER THREAD");
   return t2;
}
static Thread getWaiter() {
   Thread t0 = new Thread(new Runnable () { public void run() {
                statusThreadLock(lock, Thread.currentThread());
               try {
              synchronized(lock) {
            JFrame f = new JFrame("wait");
            f.pack();
            f.setLocationRelativeTo(text);
            JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread()));
            f.setVisible(true);
                    lock.wait();
            f.setVisible(false);
            f.dispose();
                  }
         JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread()));
      } catch(InterruptedException e) { e.printStackTrace(); }
   }}, "NOTIFY WAITER THREAD");
   return t0;
}

static boolean esc = false;
static JTextArea text;
static JScrollPane sp;
final static Object lock = new Long(System.currentTimeMillis());

public static void main(String[] args)  {

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() { public boolean postProcessKeyEvent(KeyEvent e) {
   if(e.getID() == KeyEvent.KEY_PRESSED)
      if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
         return esc = true;
   return false;
}});

JFrame f = new JFrame();
f.getContentPane().setLayout(new FlowLayout());
f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
   System.exit(0);
}});
Action action_notify = new AbstractAction("<NOTIFY>") { public void actionPerformed(ActionEvent e) {
   getNotifier().start();
}};
Action action_notifyAll = new AbstractAction("<NOTIFY ALL>") { public void actionPerformed(ActionEvent e) {
   new Thread(new Runnable() {public void run() {
      synchronized(lock) {
         lock.notifyAll();
      }
   }}).start();
}};
Action action_wait = new AbstractAction("<WAIT>") { public void actionPerformed(ActionEvent e) {
   getWaiter().start();
}};
sp = new JScrollPane(text = new JTextArea());
f.getContentPane().add(sp);
sp.setPreferredSize(new Dimension(300, 150));
text.setLineWrap(true);

f.getContentPane().add(new JButton(action_notify));
f.getContentPane().add(new JButton(action_wait));
f.getContentPane().add(new JButton(action_notifyAll));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
/** run 2 threads in the correct order */
Thread t1 = new Thread(new Runnable() { public void run() {
                   statusThreadLock(lock, Thread.currentThread());
             synchronized(lock) {
            try {
      while(!esc) {
         System.out.println("press Escape to exit !");
         lock.wait(500);
      }
            statusThreadLock(lock, Thread.currentThread());
      //lock.notify();
     System.exit(0);
      } catch(InterruptedException e) { e.printStackTrace(); }    
             }
}}, "EVENT WAITER THREAD");
t1.start();
}
}

 Cool Cool

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!