JAVA开启异步线程的方式:
1.Thread类
2.Runnable接口
3.FutureTask类
演示类说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package per.guc.gucproject.test;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Callable; import java.util.concurrent.FutureTask;
@Slf4j public class Test01 {
public static void main(String[] args) { Test01 test01 = new Test01();
test01.thread01();
test01.thread02();
test01.thread03();
}
public void thread01(){ new Thread("thread01"){ public void run(){ log.debug("创建线程方式1,启动啦。。。。"); } }.start(); }
public void thread02(){ new Thread(new Runnable() { @Override public void run() { log.debug("创建线程方式2,启动啦。。。。"); } }, "thread02").start(); }
public void thread03(){ new Thread(new FutureTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { log.debug("创建线程方式3,启动啦。。。。"); return null; } }), "thread03").start(); } }
|
运行结果:

线程的5种状态操作系统层面,线程的6种状态Java层面
1 2 3 4 5 6 7 8 9 10 11 12 13
| #操作系统层面 1.新建状态(NEW) 2.就绪状态(READ) 3.运行状态(RUNNING) 4.阻塞状态(BLOCKED) 5.死亡状态(TERMINATED) #JAVA代码层面 1.NEW(新建状态) 2.RUNNABLE(运行状态) 3.BLOCKED(阻塞状态) 4.WAITING(无时等待状态) 5.TIMED_WAITING(有时等待状态) 6.TERMINATED(死亡状态)
|


阻塞线程的方式和线程对应的状态.
演示类说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package per.guc.gucproject.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j public class Test02 {
public static void main(String[] args) {
Thread t1 = new Thread("thread01") { @Override public void run(){ log.debug("thread01 new ..."); } };
Thread t2 = new Thread("thread02") { @Override public void run() { log.debug("thread02 runnable ..."); while (true) {
} } }; t2.start();
Thread t3 = new Thread("thread03") { @Override public void run() { synchronized (Test02.class) { log.debug("thread03 time_waiting ..."); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; t3.start();
Thread t4 = new Thread("thread04") { @Override public void run() { synchronized (Test02.class) { log.debug("thread04 blocked ..."); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; t4.start();
Thread t5 = new Thread("thread05") { @Override public void run() { log.debug("thread05 waiting ..."); try { t3.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }; t5.start();
Thread t6 = new Thread("thread06") { @Override public void run() { log.debug("thread06 terminated ..."); } }; t6.start();
try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); }
log.debug("t1 state, {}", t1.getState()); log.debug("t2 state, {}", t2.getState()); log.debug("t3 state, {}", t3.getState()); log.debug("t4 state, {}", t4.getState()); log.debug("t5 state, {}", t5.getState()); log.debug("t6 state, {}", t6.getState());
} }
|
运行结果:

Thread.sleep()方法说明:
使当前执行的线程在指定的毫秒数内休眠(暂时停止执行),这取决于系统计时器和调度程序的精度和准确性,线程不会丢失任何监视器的所有权。
1 2 3 4
|
TimeUnit.MINUTES.sleep(1); TimeUnit.SECONDS.sleep(1);
|
示例及测试结果:
