还可以使用回调方法: public interface Servlet{ void callback(); } public class ServerImpl implements Server{ public void callback(){ System.out.println("线程已经执行完毕"); } public void static main(String[]){ Server server=new ServerImpl(); ClientThread client=new ClientThread(server); Thread testClient=new Thread(client); testClient.start(); } } public class ClientThread implements Runnable{ private Server server; public ClientThread(Server server){ this.server=server; } public void run(){ for(int i=0;i<1000;i++){ } server.callback(); } }
public interface Servlet{
void callback();
}
public class ServerImpl implements Server{
public void callback(){
System.out.println("线程已经执行完毕");
}
public void static main(String[]){
Server server=new ServerImpl();
ClientThread client=new ClientThread(server);
Thread testClient=new Thread(client);
testClient.start();
}
}
public class ClientThread implements Runnable{
private Server server;
public ClientThread(Server server){
this.server=server;
}
public void run(){
for(int i=0;i<1000;i++){ }
server.callback();
}
}
能不能说得稍微详细一点.
主程序;Java包含了通过wait( ),notify( )和notifyAll( )方法实现的一个进程间通信机制。wait( ) 告知被调用的线程放弃管程进入睡眠直到其他线程进入相同管程并且调用notify( )。notify( ) 恢复相同对象中第一个调用wait( ) 的线程。
它们并行执行,每个线程都执行一个"批量"任务.
我希望在这个线程都执行完之后能够"告之"主程序,
任务已经全部完成.否则我无法知道是否任务是否已经完成.
m.start();wait(); // 这样?
在wait()之后的语句,肯定是在线程结束之后
才执行的吗?线程代码
在run函数中添加 ...
public void run() {
...
notify(); // 最后一行
}是这样吗?
它是直接去调用主程序的方法
如果有多个线程的话,那又如何处理呢