//利用Runnable接口来创建线程--功能like 11_01
class MyThread implements Runnable
{
private String who;
public MyThread(String str) //构造方法
{
who=str;
}
public void run() //实现run()方法
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep((int)(1000*Math.random())); //Thread.
}
catch(InterruptedException e){}
System.out.println(who+"正在运行!");
}
}
}
public class App11_2 {
public static void main(String[] args)
{
MyThread you=new MyThread("你");
MyThread she=new MyThread("她");
Thread t1=new Thread(you); //产生Thread类的对象t1
Thread t2=new Thread(she);
t1.start(); //用t1激活线程
t2.start();
System.out.println("主方法main()运行结束!");
}
}
App11_02_利用Runnable接口来创建线程
最新推荐文章于 2020-11-23 09:37:06 发布