Results 1 to 3 of 3
Thread: synchronization question
- 08-14-2007, 09:52 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 1
- Rep Power
- 0
synchronization question
Hi everyone,
i'm new to forums and java threads.
As i was studying threads, i encountered the two sample classes below:
Java Code:class NewThread implements Runnable { String name; // name of thread Thread t; boolean suspendFlag; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); suspendFlag = false; t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for (int i = 15; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(200); synchronized (this) { while (suspendFlag) { wait(); } } } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); } void mysuspend() { suspendFlag = true; } synchronized void myresume() { suspendFlag = false; notify(); } }and there are some points i don't understant.Java Code:class SuspendResume { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); try { Thread.sleep(1000); ob1.mysuspend(); System.out.println("Suspending thread One"); Thread.sleep(1000); ob1.myresume(); System.out.println("Resuming thread One"); ob2.mysuspend(); System.out.println("Suspending thread Two"); Thread.sleep(1000); ob2.myresume(); System.out.println("Resuming thread Two"); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } // wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); } }
First why are we using synchronized as we are calling wait(). I don't see any way there could be parallel usege.
Second, when i remove the synchronized keyword from the code i get java.lang.IllegalMonitorStateException. After this if i also remove the lines with ob2 i don't get any exception. How come removing ob2 affects ob1 and prevents the program from throwing exception ?
As i said before, i'm new to threads, and if you could help me i would really appriciate it.
- 09-22-2007, 12:34 AM #2
After this if i also remove the lines with ob2 i don't get any exception. How come removing ob2 affects ob1 and prevents the program from throwing exception ?
I tried this (removing ob2) and I still got the same exceptions in the console:
The key seems to be "current thread not owner".Java Code:Exception in thread "One" java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Unknown Source) at NewThread.run(suspendresume.java:59) at java.lang.Thread.run(Unknown Source)
In the Method Summary section of the Thread class api, scroll down to the section "Methods inherited from class java.lang.Object" and find the notify and wait methods.
Look these up in the Object class api and read the information in the Method Detail for each. It specifies the conditions required for a thread to obtain a lock on, and thus be the owner of, an objects monitor.
You can test this in the NewThread class with the addition of a print statement in the run method:
and see what happens in the console when you run the app with and without commenting out the two lines for the "synchronized(this)" block.Java Code:synchronized (this) { Thread thread = Thread.currentThread(); System.out.printf("name = %s holdsLock = %b%n", thread.getName(), Thread.holdsLock(this)); while (suspendFlag) { wait(); } }
- 07-22-2008, 08:56 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
wait(),notify(),notifyAll() should always call from synchronized method or synchronized statement blocks.
Because when a wait() calls on a object the wait() releases the lock acquired by the synchronized method or block. If we do not place wait() inside synchronized method or block then it can't release lock on the object,so it will throw Exception
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Synchronization Doesn't seem to work
By sherinpearl in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:30 PM -
is synchronization on method passing local variables as parameters needed
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 04:47 PM -
Synchronization problems
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 01:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks