天天看点

java线程的两种启动方式

public class TicketRunnable implements Runnable{

//实现run方法

@Override

public void run() {

for(int i=0; i<10; i++){

System.out.println(i+"  当前的线程为  "+Thread.currentThread().getName());

}

}

public static void main(String[] args) {

//单个线程执行单个任务

TicketRunnable runnable1 = new TicketRunnable();

TicketRunnable runnable2 = new TicketRunnable();

TicketRunnable runnable3 = new TicketRunnable();

Thread t1 = new Thread(runnable1);

Thread t2 = new Thread(runnable2);

Thread t3 = new Thread(runnable3,"t3");//设置当前线程名称为runnable3

t1.start();

t2.start();

t3.start();

//两个线程执行单个任务

TicketRunnable runnable11 = new TicketRunnable();

Thread t11 = new Thread(runnable11,"t11");

Thread t22 = new Thread(runnable11,"t22");

t11.start();

t22.start();

}

}

public class TickedThread extends Thread{

//重新run方法

public void run() {

for(int i=0; i<10; i++){

System.out.println(i+"  当前的线程为  "+Thread.currentThread().getName());

}

}

public static void main(String[] args) {

//单个线程执行单个任务

TickedThread thread1 = new TickedThread();

TickedThread thread2 = new TickedThread();

TickedThread thread3 = new TickedThread();

Thread t1 = new Thread(thread1);

Thread t2 = new Thread(thread2);

Thread t3 = new Thread(thread3,"t3");//设置当前线程名称为runnable3

t1.start();

t2.start();

t3.start();

//两个线程执行单个任务

TickedThread thread11 = new TickedThread();

Thread t11 = new Thread(thread11,"t11");

Thread t22 = new Thread(thread11,"t22");

t11.start();

t22.start();

}

}

线程的状态

创建(NEW):当程序使用new关键字创建了一个线程后,该线程就处于新建状态。

就绪(可运行RUNNABLE):线程对象创建后,其他线程(比如main线程)调用了该对象的start方法后,

该状态的线程位于可运行的线程池中,等待被线程调度选中,获取CPU的使用权。此时线程启动,

进入就绪状态。当线程运行之后,从等待或者睡眠中回来后,也会处于就绪状态。

运行(RUNNING):线程调度程序将处理就绪状态的线程设置为当前线程,此时线程进入运行状态,开始

运行run函数中的代码(即可运行状态的线程获得了CPU的时间片,执行程序代码)。

阻塞(BLOCKED):线程因为某种原因放弃了CPU的使用权,即让出了CPU时间片,暂时停止运行。

直到线程进入可运行状态,才有机会再次获得CPU时间片,转到运行状态。

阻塞的情况分三种:

(一). 等待阻塞:运行(running)的线程执行o.wait()方法,JVM会把该线程放入等待队列(waitting queue)中。

(二). 同步阻塞:运行(running)的线程在获取对象的同步锁时,若该同步锁被别的线程占用,则JVM会把该线程放入锁池(lock pool)中。

(三). 其他阻塞:运行(running)的线程执行Thread.sleep(long ms)或t.join()方法,或者发出了I/O请求时,JVM会把

该线程置为阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入可运行(runnable)状态。

死亡(DEAD):线程run(),main()方法执行结束,或者调用stop方法,或者因异常退出了run方法后,则该线程结束生命周期。

死亡的线程不可再次复生。

启动线程的两种方式

1,写一个类继承Thread类,重写run方法,用start启动线程。

2,写一个类实现Runnable接口,实现run方法,用start启动线程。

多线程原理:

相当于玩游戏,只有一个游戏机(CPU),可能有很多人(线程)要玩,于是start是排队,等CPU选中你,

你就run,当CPU的运行时间片执行完,这个线程就继续排队,等待下一次的run。

调用start()后,线程会被放到等待队列,等待CPU调度,并不一定要马上开始执行,只是将这个线程

置于可动行状态。然后通过JVM,线程Thread会调用run()方法,执行本线程的线程体。先调用start

后调用run,这么麻烦,为了不直接调用run?就是为了实现多线程的优点,没这个start不行。

1.start()方法来启动线程,真正实现了多线程运行。这时无需等待run方法体代码执行完毕,

可以直接继续执行下面的代码;通过调用Thread类的start()方法来启动一个线程, 

这时此线程是处于就绪状态, 并没有运行。 然后通过此Thread类调用方法run()来完成其运行操作的, 

这里方法run()称为线程体,它包含了要执行的这个线程的内容, Run方法运行结束, 此线程终止。然后CPU再调度其它线程。

2.run()方法当作普通方法的方式调用。程序还是要顺序执行,要等待run方法体执行完毕后,才可继续执行下面的代码;

程序中只有主线程——这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。

记住:多线程就是分时利用CPU,宏观上让所有线程一起执行 ,也叫并发。

参考文档

https://blog.csdn.net/xuxurui007/article/details/7685076

https://www.cnblogs.com/echo-cheng/p/6814909.html

https://blog.csdn.net/pange1991/article/details/53860651

继续阅读