Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  All Thread locks be unlocked with wait???  (Read 1096 times)
0 Members and 1 Guest are viewing this topic.
Offline broumbroum

Junior Member





« Posted 2007-08-21 23:27:06 »

Hi! I'm just about fixing the synch for a Demo but I do have some doubts about Thread locks. Be one Thread T having N objects locked by some previous synchronized locks, then an Object O calls its own wait() method at some point in T.
 
a) Will T relinquish the N previous locks for ANYONE of the objects declared as synchronized ? YES/NO
 b) Will T relinquish the X previous locks for ONE of the objects declared as synchronized, that be O ? YES/NO

 Huh Huh Huh ( I've just been reading the last paragraph of this document from Sun which seems to get the answer for a) and b) )

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2007-08-22 00:08:22 »

a) NO
b) NO


if T1 locks object A,B,C,D and waits on C, T2 can now aquire C, but not A,B,D.


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  
         class State
         {
            volatile int value = 0;
         }

         final State state = new State();

         final Object a = "a";
         final Object b = "b";
         final Object c = "c";
         final Object d = "d";

         new Thread()
         {
            public void run()
            {
               while (state.value != 3)
                  continue;

               System.out.println("T blocked on " + a);
               synchronized (a)
               {
                  System.out.println("T aquired " + a);
                  System.out.println("T blocked on " + b);
                  synchronized (b)
                  {
                     System.out.println("T aquired " + b);
                     System.out.println("T blocked on " + c);
                     synchronized (c)
                     {
                        System.out.println("T aquired " + c);
                        System.out.println("T blocked on " + d);
                        synchronized (d)
                        {
                           System.out.println("T aquired " + d);
                           while (true)
                           {
                              try
                              {
                                 System.out.println("T [waits] on " + c);
                                 c.wait();
                                 System.out.println("T [woke] on " + c);
                              }
                              catch (InterruptedException exc)
                              {
                                 exc.printStackTrace();
                              }
                           }
                        }
                     }
                  }
               }
            }
         }.start();

         new Thread()
         {
            public void run()
            {
               while (state.value != 4)
                  continue;

               System.out.println("T:grabber -> blocked on " + c + "...");
               synchronized (c)
               {
                  System.out.println("T:grabber -> aquired " + c);
               }

               System.out.println("T:grabber -> blocked on " + d + "... darn it!");
               synchronized (d)
               {
                  System.out.println("T:grabber -> aquired " + d);
               }
            }
         }.start();

         try
         {
            System.out.println(">> GOING TO STATE 3 (T locks a,b,c,d, waits on c)");
            state.value = 3;

            Thread.sleep(1000);

            System.out.println(">> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)");
            state.value = 4;
         }
         catch (InterruptedException exc)
         {
            exc.printStackTrace();
         }


Output:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
>> GOING TO STATE 3 (T locks a,b,c,d, waits on c)
T blocked on a
T aquired a
T blocked on b
T aquired b
T blocked on c
T aquired c
T blocked on d
T aquired d
T [waits] on c
>> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)
T:grabber -> blocked on c...
T:grabber -> aquired c
T:grabber -> blocked on d... darn it!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mr_Light

Senior Member




shiny.


« Reply #2 - Posted 2007-08-22 00:36:27 »

a sidenote: Give decent names to mutexes, always lock on the lowest possible level. It must be a very very 'funky' demo considering the amount of locks involved

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2007-08-22 00:43:03 »

Yeah, I rarely have to lock on two mutexes, I've never locked on three or more... but I guess the question was about the theory behind locks, his N could have been 2 anyway.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline broumbroum

Junior Member





« Reply #4 - Posted 2007-08-22 00:46:04 »

b) answer is NO, surely. But as for a) object O is Added to the Thread wait set, and a) answer be NO would interfere with this statement from the attached document I'm quoting :
Quote from: 17.14 Wait Sets and Notification
The method wait should be called for an object only when the current thread (call it T) has already locked the object's lock. Suppose that thread T has in fact performed N lock actions that have not been matched by unlock actions. The wait method then adds the current thread to the wait set for the object, disables the current thread for thread scheduling purposes, and performs N unlock actions to relinquish the lock.
, wouldn't it?

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2007-08-22 00:49:23 »

You first have to tell me whether O is on of the N objects.

Further, X (in your first post) is undefined, and objects are never 'declared synchronized'.

It's very hard in complex topics to understand what somebody else means when the terminology is skewy.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2007-08-22 01:00:57 »

If you have run my test-app, you see that T1 only relinquishes C, not A,B,D.

About that quote from the language-spec: I don't fully understand why it says it does N unlocks, but it's clear that it ends with "to relinquish the lock" (not "locks"), so C is relinquished, by N unlock actions (which I cannot explain).

HTH Undecided

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline broumbroum

Junior Member





« Reply #7 - Posted 2007-08-22 01:34:24 »

Ok I tested the code and obviously you're correct. My question was about how far the locks were relinquished by the Thread. For the answer becomes in that case b) YES while a) is NO. let's see the test case now!
Thereafter, the "test unit" proved that it can handle one Object-locking process at a time, I mean C-wait() called on a multiple lock on A,B,C,D does give the unlocking process to N locks on C only, where A,B,D stay locked.
I'd say then to launch a new Thread if I wanted to have the lock released for A,B,D. But this is your code I modified the end to get it exited properly, as you brought the good answers. Let's see the new output :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
 java Main
>> GOING TO STATE 3 (T locks a,b,c,d, waits on c)
T blocked on a
T aquired a
T blocked on b
T aquired b
T blocked on c
T aquired c
T blocked on d
T aquired d
T [waits] on c
>> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)
T:grabber -> blocked on c...
T:grabber -> aquired c
T:grabber -> blocked on d... darn it!
T-notify's changing STATE : 5 (Main will wait 10s and exit.)
T-notify'
s notifying T locks on c.
T [woke] on c
T:grabber -> aquired d
T-notify acquired d.
Main is waiting on c for 10s then exits.
..........
And the adapted code :
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  
  class State
         {
            volatile int value = 0;
         }
class Main {        
public static void main(String[] args) {
         final State state = new State();

         final Object a = "a";
         final Object b = "b";
         final Object c = "c";
         final Object d = "d";

         new Thread()
         {
            public void run()
            {
               while (state.value != 3)
                  continue;

               System.out.println("T blocked on " + a);
               synchronized (a)
               {
                  System.out.println("T aquired " + a);
                  System.out.println("T blocked on " + b);
                  synchronized (b)
                  {
                     System.out.println("T aquired " + b);
                     System.out.println("T blocked on " + c);
                     synchronized (c)
                     {
                        System.out.println("T aquired " + c);
                        System.out.println("T blocked on " + d);
                        synchronized (d)
                        {
                           System.out.println("T aquired " + d);
                           while (state.value < 5)
                           {
                              try
                              {
                                 System.out.println("T [waits] on " + c);
                                 c.wait();
                                 System.out.println("T [woke] on " + c);
                              }
                              catch (InterruptedException exc)
                              {
                                 exc.printStackTrace();
                              }
                           }
                        }
                     }
                  }
               }
            }
         }.start();

         new Thread()
         {
            public void run()
            {
               while (state.value != 4)
                  continue;

               System.out.println("T:grabber -> blocked on " + c + "...");
               synchronized (c)
               {
                  System.out.println("T:grabber -> aquired " + c);
               }

               System.out.println("T:grabber -> blocked on " + d + "... darn it!");
               synchronized (d)
               {
                  System.out.println("T:grabber -> aquired " + d);
               }
            }
         }.start();
   
   new Thread(new Runnable() { public void run() {
      while (state.value != 4)
              continue;
      System.out.println("T-notify's changing STATE : 5 (Main will wait 10s and exit.)");
      state.value = 5;
      synchronized(c) {
         System.out.println("T-notify's notifying T locks on c.");
         c.notify();
           }
      synchronized(d) {
         System.out.println("T-notify acquired d.");
       }
   }}).start();
         try
         {
            System.out.println(">> GOING TO STATE 3 (T locks a,b,c,d, waits on c)");
            state.value = 3;

            Thread.sleep(1000);

            System.out.println(">> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)");
            state.value = 4;
       while(state.value < 5);
        synchronized(c) {
         System.out.println("Main is waiting on c for 10s then exits. ");
         long time = System.currentTimeMillis();
         long elapsed = 0;
         do {
            if(elapsed % 1000 >= 0 && elapsed % 1000 <= 10)
               System.out.print(".");
            c.wait(10);
         }while((elapsed = System.currentTimeMillis() - time) < 10000);
      }  
         }
         catch (InterruptedException exc)
         {
            exc.printStackTrace();
         } finally {      
      System.exit(0);
      }
}
}
Thank you for your help!

::::... :..... :::::: ;;;:::™ 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.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!