[HOMEWORK,NO EFFORT,RE-POSTED]如何:2个线程完成任务后互相通知 [英] [HOMEWORK, NO EFFORT, RE-POSTED] how to: 2 threads notifying each other once they are finished doing their task

查看:90
本文介绍了[HOMEWORK,NO EFFORT,RE-POSTED]如何:2个线程完成任务后互相通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[重新发布为 java中的多线程 [ ^ ] - SA]



i有两个帖子..

thread1启动并调用thread2执行任务

一旦thread2完成,通知thread1

thread1收到thread2通知..



thread1向thread2请求另一个任务

thread2执行它并在完成后通知thread1

thread1接收通知并向thread2请求另一个任务

等等..



我将如何做到这一点在java?





谢谢,



发问者添加:



[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:

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希望输出为:



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

推荐答案

你有多少钱?

请发布代码,这是经典的功课。我们不做作业了。



但我们会在你的代码上回答你的具体问题。
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.


这篇关于[HOMEWORK,NO EFFORT,RE-POSTED]如何:2个线程完成任务后互相通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆