Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-14-2007, 10:52 PM
Member
 
Join Date: Aug 2007
Posts: 1
oguz is on a distinguished road
synchronization question
Hi everyone,
i'm new to forums and java threads.
As i was studying threads, i encountered the two sample classes below:
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(); } }
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."); } }
and there are some points i don't understant.
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-22-2007, 01:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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:
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)
The key seems to be "current thread not owner".
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:
Code:
synchronized (this) { Thread thread = Thread.currentThread(); System.out.printf("name = %s holdsLock = %b%n", thread.getName(), Thread.holdsLock(this)); while (suspendFlag) { wait(); } }
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronization Doesn't seem to work sherinpearl Threads and Synchronization 1 04-23-2008 07:30 PM
Thread priorities, synchronization and messaging JavaForums Java Blogs 0 12-17-2007 01:21 PM
is synchronization on method passing local variables as parameters needed reddzer Java Servlet 0 11-10-2007 05:47 PM
Synchronization problems Jack Advanced Java 2 07-02-2007 02:17 AM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 04:17 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org