被调用的线程运行完之后会自动运行调用自己线程(此线程被挂起)被挂起后面的部分程序吗
package newPackage;
import java.util.*;
class Waiter implements Runnable{
private int number;
private order od;
private Thread th3;
public Waiter(int num,order ord){
this.number=num;
this.od=ord;
th3=new Thread(new chef(this.number,this.od,this));
}
public synchronized void run(){
try{
if(this.od!=null){
synchronized(th3){
th3.start();
System.out.println(this.number+"号菜单已经提交服务员,正转给厨师");
if(th3.isAlive()){
th3.wait();}
System.out.println(this.number+"号饭菜已经做好,正传给客人");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setTh(Thread ce){
this.th3=ce;
}
public Thread getTh(){
return this.th3;
}
}
class chef implements Runnable{
private int number;
private order od;
private Waiter waiterx;
private Thread thx;
public chef(int num,order ord,Waiter wa){
this.number=num;
this.od=ord;
this.waiterx=wa;
}
public synchronized void run(){
try{
if(this.od!=null){
synchronized(this.waiterx.getTh()){
System.out.println("厨师正在给第"+this.number+"号客人做饭");
this.waiterx.getTh().notify();//这里有问题,不用这样的显示唤醒,waiter线程wait后面的部分也能被唤醒,这是为什么?
}
}
else{
System.out.println("厨师正在给第"+this.number+"号客人做饭");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class order implements Runnable{
private Server cutomer;
private Waiter waterx;
private order odd;
private int number;
private Server s;
private String[] args;
public order(Server ctom){
this.cutomer=ctom;
}
public synchronized void run(){
try{
this.odd=this.cutomer.getOrederx();
this.number=this.cutomer.getNumber();
if(this.cutomer!=null){
this.waterx=new Waiter(this.number,this.odd);
Thread th2=new Thread(this.waterx);
synchronized(th2){
System.out.println("这位客人的编号是"+this.number);
th2.start();
if(th2.isAlive()){
th2.wait();}
System.out.println(this.number+"号客人已经接到饭菜");
if(this.number<=9){
s=new Server();
s.setNumber(++this.number);
s.main(args);}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class Server {
private order orederx;
private static int count=0;
public static void main(String args[]){
try{
Server s=new Server();
s.setOrederx(new order(s));
Thread th1=new Thread(s.orederx);
synchronized(th1){
System.out.println("你好"+s.getNumber()+"号客人");
th1.start();
if(th1.isAlive()){
th1.wait();}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setOrederx(order od)
{
this.orederx=od;
}
public order getOrederx(){
return this.orederx;
}
public void setNumber(int num){
this.count=num;
}
public int getNumber(){
return this.count;
}
}
疑问写在代码中了,(这个程序执行没有题 |