Java实现多线程的两种方式分别是继承Thread类和实现Runnable接口。
- 继承Thread类
class MyThread extends Thread{
@Override
public void run(){
for(int i=0;i<7;i++){
System.out.println("MyThread:"+Thread.currentThread().getName()+" 次数:"+i);
}
}
}
- 实现Runnable接口
class MyTask implements Runnable{
@Override
public void run(){
for(int i=0;i<7;i++){
System.out.println("MyTask:"+Thread.currentThread().getName()+" 次数:"+i);
}
}
}
- 测试
public class Test {
public static void main(String [] args){
MyThread myThread = new MyThread();
myThread.start();
MyTask task = new MyTask();
Thread thread = new Thread(task);
thread.start();
}
}
MyThread:Thread-0 次数:0
MyThread:Thread-0 次数:1
MyThread:Thread-0 次数:2
MyThread:Thread-0 次数:3
MyThread:Thread-0 次数:4
MyThread:Thread-0 次数:5
MyTask:Thread-1 次数:0
MyThread:Thread-0 次数:6
MyTask:Thread-1 次数:1
MyTask:Thread-1 次数:2
MyTask:Thread-1 次数:3
MyTask:Thread-1 次数:4
MyTask:Thread-1 次数:5
MyTask:Thread-1 次数:6
程序会在不同的线程之间切换,每次的输出结果并不相同。
关键点1:启动线程使用start()方法,使用run()方法并不会启动新线程。
public class Test {
public static void main(String [] args){
MyThread myThread = new MyThread();
myThread.run();
MyTask task = new MyTask();
Thread thread = new Thread(task);
thread.run();
}
}
MyThread:main 次数:0
MyThread:main 次数:1
MyThread:main 次数:2
MyThread:main 次数:3
MyThread:main 次数:4
MyThread:main 次数:5
MyThread:main 次数:6
MyTask:main 次数:0
MyTask:main 次数:1
MyTask:main 次数:2
MyTask:main 次数:3
MyTask:main 次数:4
MyTask:main 次数:5
MyTask:main 次数:6
start() : 通过调用start0()启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用。
run(): run()和普通的成员方法一样可重复调用。单独调用run()会在当前线程中执行run(),并不会启动新线程!
相关源码:
public synchronized void start() {
if (threadStatus != 0) //start()方法不能被重复调用,否则会抛出异常
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this); //加入到线程组中
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
}
private native void start0();
关键点2:如何选择Thread与Runnable.
其实Thread同样实现了Runnable接口。
class Thread implements Runnable {
//…
public void run() {
if (target != null) {
target.run();
}
}
}
Thead的作用是启动新的线程,而Runnable的作用表示执行的任务。 (很好的体现了接口的原则:功能抽离;)
继承Thead类表示我们需要一种特殊的Thead;而实现Runnable接口表示我们需要执行某种任务;
Thread与Runnable的区别:
- 由于Java使用单继承,多接口实现原则,所以通过Runnable接口实现多线程更具灵活性;
- Runnable对象可以被多个线程共享,更适合于多个线程处理同一资源的情况。
(正确共享资源仍然需要加锁)
示例(卖票问题):
- Thread
class MyThread extends Thread{
private int tickets =5;
@Override
public void run() {
while (tickets>0) {
tickets--;
System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets);
}
}
}```
``` java
public class Test {
public static void main(String [] args){
MyThread myThread1 = new MyThread();
myThread1.start();
MyThread myThread2 = new MyThread();
myThread2.start();
}
}
Thread-0卖了一张票,剩余票数:4
Thread-1卖了一张票,剩余票数:4
Thread-1卖了一张票,剩余票数:3
Thread-0卖了一张票,剩余票数:3
Thread-1卖了一张票,剩余票数:2
Thread-1卖了一张票,剩余票数:1
Thread-1卖了一张票,剩余票数:0
Thread-0卖了一张票,剩余票数:2
Thread-0卖了一张票,剩余票数:1
Thread-0卖了一张票,剩余票数:0
MyThread myThread2 = new MyThread();```
因为每次都创建了新的MyThread对象,内存中有两份tickets,所以最后结果是卖了10张票;
1. Runnable
``` java
class MyRunnable implements Runnable{
private int tickets =5;
@Override
public void run() {
while (tickets>0) {
tickets--;
System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets);
}
}
}
public class Test {
public static void main(String [] args){
MyRunnable r = new MyRunnable();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
Thread-0卖了一张票,剩余票数:4
Thread-1卖了一张票,剩余票数:3
Thread-0卖了一张票,剩余票数:2
Thread-0卖了一张票,剩余票数:1
Thread-0卖了一张票,剩余票数:0
MyRunnable对象只有一个,多线程共享同一对象,内存中只有1份tickets,卖票结果好像是正确的。
- synchronized加锁
class MyRunnable implements Runnable{
private int tickets =5;
@Override
public void run() {
while (tickets>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
tickets--;
System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets);
}
}
}
Thread-0卖了一张票,剩余票数:4
Thread-1卖了一张票,剩余票数:4
Thread-1卖了一张票,剩余票数:3
Thread-0卖了一张票,剩余票数:2
Thread-0卖了一张票,剩余票数:0
Thread-1卖了一张票,剩余票数:0
显然一共卖了6张票是不正确的,所以仅仅实现Runnable接口是无法进行资源共享的,如果需要共享还是需要加入synchronized关键字。
class MyRunnable implements Runnable{
private int tickets =5;
@Override
public void run() {
synchronized(this){
while (tickets>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
tickets--;
System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets);
}
}
}
}
Thread-0卖了一张票,剩余票数:4
Thread-0卖了一张票,剩余票数:3
Thread-0卖了一张票,剩余票数:2
Thread-0卖了一张票,剩余票数:1
Thread-0卖了一张票,剩余票数:0
[2015-08-20]