Click here to Skip to main content
15,861,168 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
[Re-posted as multi threading in java[^] — SA]

i have 2 threads..
thread1 is started and calls thread2 to perform task
once thread2 is finished, notify thread1
thread1 receives thread2 notification..

thread1 requests another task to thread2
thread2 performs it and notifies thread1 once its done
thread1 receives notification and requests another task to thread2
and so on..

how will i do this in java?


thanks,

Questioner added:

Java
package testpackage;
import java.util.Date;
public class Main {
	private static class MyThreadClass extends Thread {
	    private final boolean shouldWait;      
	    private final Object someSharedResource;
	    public MyThreadClass(final Object someSharedResource, final boolean shouldWait) {
	      this.someSharedResource = someSharedResource;
	      this.shouldWait = shouldWait;
	    }
	    public void run() {
	      while(PerformTask());
	    }
	    public boolean PerformTask()
	    {
	    	
	    	final String name = Thread.currentThread().getName();
		      System.out.println("Thread " + name + " is running at " + new Date());
		      if (shouldWait) {
		      System.out.println("Thread " + name + " is waiting for sharedResource to be notified... at " + new Date());          
		        synchronized(someSharedResource) {
		          try {
		            someSharedResource.wait();
		        System.out.println("Thread " + name + " was woken up at " + new Date());
		         }
		          catch(InterruptedException e) {
		            System.out.println("Thread " + name + " was interrupted!");          
		          }
		        }
		      }
		      
		      try {
		        Thread.sleep(2000);
		      }
		      catch(InterruptedException e) {
		      }
		      System.out.println("Thread " + name + " is done at " + new Date());        
 
		      synchronized(someSharedResource) {
		        someSharedResource.notifyAll();
		        
		      }
		      
		      return true;
	    }
	  }
 
		public static void main(String[] args) throws Exception {
	    final Object sync = new Object();
	    
	    final MyThreadClass t1 = new MyThreadClass(sync, true);
	    final MyThreadClass t2 = new MyThreadClass(sync, false);
 
	    t1.setName("thread1");
	    t1.start();
	    t2.setName("thread2");
	    t2.start();
	  }
}


this code outputs:
Thread thread1 is running at Mon Aug 12 13:39:50 SGT 2013
Thread thread1 is waiting for sharedResource to be notified... at Mon Aug 12 13:39:50 SGT 2013
Thread thread2 is running at Mon Aug 12 13:39:50 SGT 2013
Thread thread2 is done at Mon Aug 12 13:39:52 SGT 2013
Thread thread2 is running at Mon Aug 12 13:39:52 SGT 2013
Thread thread1 was woken up at Mon Aug 12 13:39:52 SGT 2013
Thread thread1 is done at Mon Aug 12 13:39:54 SGT 2013


i want the output to be:
Thread thread1 is running at Mon Aug 12 13:39:50 SGT 2013
Thread thread1 is waiting for sharedResource to be notified... at Mon Aug 12 13:39:50 SGT 2013
Thread thread2 is running at Mon Aug 12 13:39:50 SGT 2013
Thread thread2 is done at Mon Aug 12 13:39:52 SGT 2013
Thread thread1 was woken up at Mon Aug 12 13:39:52 SGT 2013
Thread thread1 is done at Mon Aug 12 13:39:54 SGT 2013
Thread thread2 is running at Mon Aug 12 13:39:52 SGT 2013

how will i do this? thanks
Posted
Updated 12-Aug-13 4:25am
v4
Comments
Sergey Alexandrovich Kryukov 11-Aug-13 22:40pm    
What is "call a thread"? A thread is not a method, function, procedure, subroutine, property or operator...
—SA
dinahgee1419 12-Aug-13 1:00am    
i mean thread1 is started and thread2 is also started. thread2 performs task and lets thread1 wait. once thread2 is done, it notifies thread1 that it is finished. once thread1 receives the notification, it starts a new task again and thread2 is also started.. and so on.
dinahgee1419 11-Aug-13 22:49pm    
i mean thread1 is started and thread2 is also started. thread2 performs task and lets thread1 wait. once thread2 is done, it notifies thread1 that it is finished. once thread1 receives the notification, it starts a new task again and thread2 is also started.. and so on.
Thanks7872 12-Aug-13 0:34am    
Use reply button,so that Sergey gets notification about your comment.
TorstenH. 12-Aug-13 8:37am    
changed tags - ths is no Android, but classic homework.

1 solution

How much do you have?
Please post code, this is classic homework. we do not do homework (anymore).

But we will answer your specific questions on YOUR code.
 
Share this answer
 
Comments
dinahgee1419 13-Aug-13 2:41am    
how to do this in java.. should not start thread unless waiting thread is finished executing java
TorstenH. 13-Aug-13 3:12am    
Check This: Java Threads @ Oracle.com
Please bookmark that page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900