线程的五大状态分别为:
-
新建状态(New): 当用new操作符创建一个线程时, 例如new Thread(r),线程还没有开始运行,此时线程处在新建状态。 ...
-
就绪状态(Runnable) ...
-
运行状态(Running) ...
-
阻塞状态(Blocked) ...
-
死亡状态(Dead)
线程休眠,sleep
package com.cl.state;
//模拟网络延时,作用:放大问题发生性
public class TestSleep implements Runnable{
//票数
private int tickNums=10;
@Override
public void run() {
while (true){
if (tickNums<=0){
break;
}
//模拟延时
try{
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+tickNums--+"票");
}
}
public static void main(String[] args) {
TestSleep ticket = new TestSleep();
new Thread(ticket,"king").start();
new Thread(ticket,"king2").start();
new Thread(ticket,"king3").start();
}
}
练习:利用sleep模拟倒计时和实时获取系统当前时间
package com.cl.state;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class TestSleep2 {
public static void main(String[] args) {
//打印当前系统时间
Date startTime=new Date(System.currentTimeMillis());//获取系统当前时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//模拟倒计时
public static void tenDown() throws InterruptedException{
int num=10;
while (true){
Thread.sleep(100);
System.out.println(num--);
if (num<=0){
break;
}
}
}
}
//模拟倒计时的main
/*
* tenDown();直接调用
/
线程停止,stop
package com.cl.state;
//线程停止
//测试stop
//1.建议线程正常停止---->利用次数,不建议死循环
//2.建议使用标志位--->设置一个标志位
//3.不要使用stop或者destory等过时或者jdk不建议使用的方法
public class TestStop implements Runnable {
//1.设置一个标志位
private boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run....Thread"+i++);
}
}
//2.设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if (i==900){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程该停止了");
}
}
}
}
线程礼让,yield
package com.cl.state;
//测试礼让线程
//礼让不一定成功,看cpu心情
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
线程-强制执行,jion
package com.cl.state;
//测试join方法,强制执行
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动主线程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 1000; i++) {
if (i==200){
thread.join();//插队
}
System.out.println("main"+i);
}
}
}
用户线程(eg:mian),守护线程(eg:gc,垃圾回收)
package com.cl.state;
//测试守护线程
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread();
thread.setDaemon(true);//默认是false表示用户线程
thread.start();//守护线程
new Thread(you).start();//用户线程
}
}
//守护
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("守护着你");
}
}
}
//被守护
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30000; i++) {
System.out.println("开心的活着");
}
System.out.println("hello world");
}
}