线程的状态:
1.线程也有固定的操作状态:
创建状态:准备好了一个多线程的对象(也就是new出来一个Thread对象)
就绪状态:调用了start()方法,等待CPU进行调度(等待CPU分配资源的状态)
运行状态:执行run()方法(当CPU分配好资源或CPU已经进行调度,此时处于运行状态)
阻塞状态:暂时停止执行,可能将资源交给其他线程使用(可再恢复到运行状态)
终止状态(死亡状态):线程销毁(最终当整个线程执行完毕之后,线程自动销毁)
线程的常用方法:
线程的常用方法大都存在于Thread类当中,所以大多时候我们通过Thread类进行调用。
1.取得线程名称
getName()
2.取得当前线程对象
currentThread()
3.判断线程是否启动
isAlive()
4.线程的强行运行
join()
5.线程的休眠
sleep()
6.线程的礼让(针对声明周期是非常重要的)
yield()
代码演示:(1、2)
package com.yeqc.thread;class RunableDemo implements Runnable{ private String name; public RunableDemo(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println("当前线程对象:"+Thread.currentThread().getName()); } } }public class ThreadDemo02 { public static void main(String[] args) { RunableDemo r1 = new RunableDemo("A"); RunableDemo r2 = new RunableDemo("B"); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); }}
运行结果:
当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-0当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1当前线程对象:Thread-1
代码演示:(3.判断线程是否启动)
package com.yeqc.thread;class RunableDemo implements Runnable{ private String name; public RunableDemo(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println(name+":"+i); } } }public class ThreadDemo02 { public static void main(String[] args) { RunableDemo r = new RunableDemo("A"); Thread t = new Thread(r); System.out.println(t.isAlive()); t.start(); System.out.println(t.isAlive()); }}
运行结果:
falsetrueA:0A:1A:2A:3A:4A:5A:6A:7A:8A:9A:10A:11A:12A:13A:14A:15A:16A:17A:18A:19A:20A:21A:22A:23A:24A:25A:26A:27A:28A:29A:30A:31A:32A:33A:34A:35A:36A:37A:38A:39A:40A:41A:42A:43A:44A:45A:46A:47A:48A:49
代码演示(4.线程的强行运行)
package com.yeqc.thread;class RunableDemo implements Runnable{ private String name; public RunableDemo(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println(name+":"+i); } } }public class ThreadDemo02 { public static void main(String[] args) { RunableDemo r = new RunableDemo("A"); Thread t = new Thread(r); t.start(); for(int i=0; i<50; i++){ if (i>10) { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("主线程:"+i); } }}
运行结果:
主线程:0A:0主线程:1A:1主线程:2A:2主线程:3A:3主线程:4A:4主线程:5A:5主线程:6A:6主线程:7A:7主线程:8A:8主线程:9A:9主线程:10A:10A:11A:12A:13A:14A:15A:16A:17A:18A:19A:20A:21A:22A:23A:24A:25A:26A:27A:28A:29A:30A:31A:32A:33A:34A:35A:36A:37A:38A:39A:40A:41A:42A:43A:44A:45A:46A:47A:48A:49主线程: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
代码演示(5.线程的休眠)
package com.yeqc.thread;class RunableDemo implements Runnable{ private String name; public RunableDemo(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 50; i++) { try { Thread.sleep(1000); System.out.println(name+":"+i); } catch (InterruptedException e) { e.printStackTrace(); } } } }public class ThreadDemo02 { public static void main(String[] args) { RunableDemo r = new RunableDemo("A"); Thread t = new Thread(r); t.start(); }}
运行结果(每1s执行一次)
A:0A:1A:2A:3A:4A:5A:6A:7A:8A:9A:10A:11A:12A:13A:14A:15A:16A:17A:18A:19A:20A:21A:22A:23A:24A:25A:26A:27A:28A:29A:30A:31A:32A:33A:34A:35A:36A:37A:38A:39A:40A:41A:42A:43A:44A:45A:46A:47A:48A:49
代码演示(6.线程的礼让)
package com.yeqc.thread;class RunableDemo implements Runnable{ private String name; public RunableDemo(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println(name+":"+i); if (i == 10) { System.out.println("礼让"); Thread.yield(); } } } }public class ThreadDemo02 { public static void main(String[] args) { RunableDemo r1 = new RunableDemo("A"); RunableDemo r2 = new RunableDemo("B"); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); }}
运行结果:
A:0B:0A:1A:2A:3B:1A:4B:2A:5B:3A:6B:4A:7B:5A:8B:6A:9B:7A:10B:8礼让B:9A:11B:10礼让A:12B:11A:13B:12A:14B:13A:15B:14A:16B:15A:17B:16A:18B:17A:19B:18A:20B:19A:21B:20A:22B:21A:23B:22A:24B:23A:25B:24A:26B:25A:27A:28B:26A:29B:27A:30B:28A:31B:29A:32B:30A:33B:31A:34B:32A:35B:33A:36B:34A:37B:35A:38B:36A:39B:37A:40B:38A:41B:39A:42B:40A:43B:41A:44B:42A:45A:46B:43B:44B:45B:46A:47A:48B:47B:48B:49A:49