您的位置:首页 > 其它

通过实现Runnable接口实现多线程

2014-11-27 15:33 225 查看
//通过实现Runnable接口实现多线程package 多线程;
class TestThread_01 implements Runnable{    public void run()    {        for(int i = 0 ; i < 10 ; i++)        {            System.out.println("TestThread_01在运行");        }    }}public class ThreadDemo_02 {    public static void main(String[] args) {        TestThread t = new TestThread();        new Thread(t).start();//启动线程,即启动了run()方法//        或者是下面的方法。一个实现了Runnable接口的类的对象,可以用它来创建多个线程//        在Runnable接口中只有一个run()方法而没有start()方法。所以在实现了Runnable接口//        也必须用Thread类的start()方法来启动多线程。Thread类中的构造方法为://        public Thread(Runnable target);将一个Runnable接口的实例化对象作为参数去实例化Thread//        类对象。//        TestThread t = new TestThread();//        Thread thread1 = new Thread(t);//        thread1.start();//        Thread thread2 = new Thread(t);//        thread2.start();        for(int i = 0 ; i < 10 ; i++)        {            System.out.println("main在运行");        }    }}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: