Thread和Runnable
1、简介
Runnable是一个接口,该接口中只包含了一个run()方法。它的定义为:
public interfaceRunnable
{public abstract voidrun()
}
Runnable的作用为实现多线程。我们可以定义一个类A,实现Runnable接口;然后通过new Thread(new A())方式新建线程
Thread是一个类,它背身就实现了Runnable接口。它的声明为:
public class Thread implements Runnable{}
Thread类的作用,也是实现多线程
2、Thread和Runnable的异同点
相同点:都是“多线程”的实现方式
不同点:(1)、Thread是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。而一个类只能继承一个父类,却可以实现多个接口,因此Runnable具有更好的扩展性;(2)、Runnable还可以用于“资源共享”,即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。
3、Thread的多线程示例
class MyThread extendsThread
{private int ticket = 10;public voidrun()
{for(int i = 0;i < 20;i++)
{if(this.ticket > 0)
System.out.println(this.getName()+" sells a ticket. Before this we have:"+this.ticket--);
}
}
}public classproblem1
{public static voidmain(String[] args)
{
MyThread t1= newMyThread();
MyThread t2= newMyThread();
MyThread t3= newMyThread();
t1.start();
t2.start();
t3.start();
}
}
运行结果为:
Thread-1 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:9Thread-0 sells a ticket. Before this we have:8Thread-0 sells a ticket. Before this we have:7Thread-0 sells a ticket. Before this we have:6Thread-0 sells a ticket. Before this we have:5Thread-0 sells a ticket. Before this we have:4Thread-0 sells a ticket. Before this we have:3Thread-0 sells a ticket. Before this we have:2Thread-2 sells a ticket. Before this we have:10Thread-2 sells a ticket. Before this we have:9Thread-2 sells a ticket. Before this we have:8Thread-2 sells a ticket. Before this we have:7Thread-2 sells a ticket. Before this we have:6Thread-2 sells a ticket. Before this we have:5Thread-2 sells a ticket. Before this we have:4Thread-2 sells a ticket. Before this we have:3Thread-2 sells a ticket. Before this we have:2Thread-2 sells a ticket. Before this we have:1Thread-0 sells a ticket. Before this we have:1Thread-1 sells a ticket. Before this we have:9Thread-1 sells a ticket. Before this we have:8Thread-1 sells a ticket. Before this we have:7Thread-1 sells a ticket. Before this we have:6Thread-1 sells a ticket. Before this we have:5Thread-1 sells a ticket. Before this we have:4Thread-1 sells a ticket. Before this we have:3Thread-1 sells a ticket. Before this we have:2Thread-1 sells a ticket. Before this we have:1
结果说明:
(1)、MyThread类继承于Thread类,它是自定义的线程。每个MyThread会卖出10张票
(2)、主线程main创建并启动了3个MyThread子线程。每个子线程都各自卖出了10张票
4、Runnable的多线程示例
class MyThread implementsRunnable
{private int ticket = 10;public voidrun()
{for(int i = 0;i < 20;i++)
{if(this.ticket > 0)
System.out.println(Thread.currentThread().getName()+ " sells a ticket. Before this we have:"+this.ticket--);
}
}
}public classproblem1
{public static voidmain(String[] args)
{
MyThread mt= newMyThread();
Thread t1= newThread(mt);
Thread t2= newThread(mt);
Thread t3= newThread(mt);
t1.start();
t2.start();
t3.start();
}
}
运行结果为:
Thread-0 sells a ticket. Before this we have:10Thread-0 sells a ticket. Before this we have:7Thread-2 sells a ticket. Before this we have:8Thread-2 sells a ticket. Before this we have:5Thread-1 sells a ticket. Before this we have:9Thread-2 sells a ticket. Before this we have:4Thread-0 sells a ticket. Before this we have:6Thread-2 sells a ticket. Before this we have:2Thread-1 sells a ticket. Before this we have:3Thread-0 sells a ticket. Before this we have:1
结果说明:
(1)、和上面的MyThread类继承于Thread类不同,这里的MyThread实现了Runnable接口
(2)、主线程main创建并启动了三个子线程,而且这三个子线程都是基于"mt"这个Runnable对象创建的。运行结果是这三个子线程一共卖出了10张票,这说明它们是共享资源的