Results 1 to 4 of 4
  1. #1
    fgstat is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default Simple multithreading using wait and notify

    Can anyone read the following code and see why I can't let 3 threads count to 60? Order in which they execute (increment to 20) is not important but most of the times I only see two of them do. The only condition is I have to use wait and notify to make it work.

    Java Code:
    class MyBetterThread extends Thread {
    	
    	  private static int count = 0;
    	  private static boolean can_go = true;
    	   
    	  private String name;	
    	   
    	  public void incrementby(int j)throws InterruptedException {
    	   
    		  synchronized (this) {
    			  while (!can_go) {
    				  wait();  // release the lock of this object
    			  }
    			  can_go = false;
    			  for (int i = 1; i <= j; ++i) {  // try 5, 10, 20
    				  count += 1;
    				  System.out.println("Count from " + name + ": " + count);
    			  }
    			  can_go = true;
    			  notify();
    		  }
    	   }
      
    	   public MyBetterThread(String name) {   // constructor
    	      this.name = name;
    	   }
    	   
    	   @Override
    	   public void run() {
    		   try {
    			   incrementby(20);
    		   } catch (InterruptedException e) {} 
    	  }
    }
    
    class MyBetterThreadTest {
        public static void main(String[] args) {
            Thread[] mybetterthreads = {
               new MyBetterThread("Thread 1"),
               new MyBetterThread("Thread 2"),
               new MyBetterThread("Thread 3")
            };
            for (Thread t : mybetterthreads) {
               t.start();
            }
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Simple multithreading using wait and notify

    Well, I am not an expert on threads but I do not believe your synchronization is correct (or even necessary). The idea behind synchronization is to allow multiple threads to manipulate the same object without interferring with each other. But each of your threads is simply running a different instantiation simultaneously. Their only common values are the statics.

    Here is how most threads (at least from my perspective are run) {
    Java Code:
    MyClass myClass = new MyClass(); // implements runnable
    
    new Thread(myClass, "Thread 1").start();
    new Thread(myClass,"Thread 2").start();
      // and so forth
    Regards,
    Jim
    Last edited by jim829; 07-26-2013 at 04:21 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Simple multithreading using wait and notify

    What is the point of the can_go checks and the notify/wait stuff?
    'can_go' can only ever be true when a thread enters the synch block, surely?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,869
    Rep Power
    5

    Default Re: Simple multithreading using wait and notify

    synchronizing on "this" does not seem the way to go though, you'll want to synchronize on exactly the same object for each and every thread instance and now each thread instance is obtaining a lock on itself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Wait() and Notify()
    By SiX in forum New To Java
    Replies: 15
    Last Post: 07-28-2011, 04:29 PM
  2. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 08:58 AM
  3. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  4. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  5. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •