黑马程序员技术交流社区
标题:
多线程写4位老师分发80分试卷
[打印本页]
作者:
薛大千
时间:
2015-4-9 22:57
标题:
多线程写4位老师分发80分试卷
class ThreadTest
{
public static void main(String[] args)
{ SendPaper a=new SendPaper();
Thread th1= new Thread(a,"老师1");
Thread th2= new Thread(a,"老师2");
Thread th3= new Thread(a,"老师3");
Thread th4= new Thread(a,"老师4");
th1.start();
th2.start();
th3.start();
th4.start();
}
}
class SendPaper implements Runnable//继承Runnable接口
{Object lock =new Object();
public void run()
{synchronized(lock)//同步锁标记
{
try{
for (int x=1;x<=80 ;x++ )
{System.out.println(Thread.currentThread().getName()+"发送第"+x+++"卷子");
Thread.sleep(100);}
}
catch(InterruptedException e){
e.printStackTrace();}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2