App11_02_利用Runnable接口来创建线程

来源:互联网 发布:商品期货交易软件下载 编辑:程序博客网 时间:2024/04/29 07:50
//利用Runnable接口来创建线程--功能like 11_01class 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类的对象t1Thread t2=new Thread(she);t1.start();   //用t1激活线程t2.start();System.out.println("主方法main()运行结束!");  }}

0 0
原创粉丝点击