1、终止线程
使用标识终止线程
/**
* 终止线程
* 1、线程正常完毕
* 2、
* @author AnQi
* @date 2020/3/6 21 59:42
* @description
*/
public class TerminateThread implements Runnable {
//1、加入表示标记线程体是否可以运行
private boolean flag = true;
private String name ;
public TerminateThread(String name) {
this.name = name;
}
@Override
public void run() {
int i = 0;
//2、关联标识 true->运行 false->停止
while(flag){
System.out.println(name +"-->"+ i++);
}
}
//3、对外提供方法改变标识
public void terminate(){
this.flag=false;
}
public static void main(String[] args) {
TerminateThread tt = new TerminateThread("C罗");
new Thread(tt).start();
for(int i = 0;i<99;i++){
if(i==88){
tt.terminate();
System.out.println("tt game over~~");
}
System.out.println("main-->"+ i );
}
}
}
2、yield 礼让线程
/**
* yield 礼让线程 直接由运行->就绪状态
* @author AnQi
* @date 2020/3/6 22 38:29
* @description
*/
public class YieldDemo01 {
public static void main(String[] args) {
MyYield my= new MyYield();
new Thread(my,"qqq").start();
new Thread(my,"rrr").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--> start");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"--> end");
}
}
/**
* @author AnQi
* @date 2020/3/6 22 56:07
* @description
*/
public class YieldDemo02 {
public static void main(String[] args) {
new Thread(()->{
for(int i = 0;i <100;i++){
System.out.println("lambda"+ i);
}
}).start();
for(int i=0;i<100;i++)
{
if(i%20 == 0){
Thread.yield(); //mian线程礼让
}
System.out.println("main"+ i);
}
}
}
3、sleep
- 拿着资源进入运行->阻塞状态 时间结束 阻塞状态 ->就绪状态 等待CPU调用 进入运行状态
sleep 模拟网络延迟
/**
* sleep 模拟网络延迟
* @author AnQi
* @date 2020/3/6 22 16:45
* @description
*/
public class BlockedSleep01 {
public static void main(String[] args) {
//一份资源
Web12306 web=new Web12306();
System.out.println(Thread.currentThread().getName());
//多个代码
new Thread(web,"1one").start();
new Thread(web,"2two").start();
new Thread(web,"3three").start();
}
static class Web12306 implements Runnable{
private int tickNums=99;
public void run() {
// TODO Auto-generated method stub
while(true ) {
if(tickNums<0) {
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->"+tickNums--);
}
}
}
sleep 模拟休息
/**
* sleep 模拟休息
* @author AnQi
* @date 2020/3/6 22 16:45
* @description
*/
public class BlockedSleep02 {
static class Racer implements Runnable{
public static void main(String[] args) {
cn.qqqking.thread.Racer racer=new cn.qqqking.thread.Racer();
new Thread(racer,"tortoise").start();
new Thread(racer,"rabbit").start();
}
private static String winner;//胜利者
@Override
public void run() {
// TODO Auto-generated method stub
for(int steps=1;steps<=100;steps++)
{
//模拟休息
if(Thread.currentThread().getName().equals("rabbit")&&steps%10==0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"-->"+steps);
//比赛是否结束
boolean flag=gameOver(steps);
if(flag) {
break;
}
}
}
private boolean gameOver(int steps) {
if (winner!=null) {//存在胜利者
return true;
}else {
if(steps==100) {
winner=Thread.currentThread().getName();
System.out.println("winner==>"+winner);
return true;
}
}
return false;
}
}
}
public class BlockedSleep03 {
public static void main(String[] args) throws InterruptedException {
//倒计时
Date endTime = new Date(System.currentTimeMillis()+1000*10);
long end = endTime.getTime();
while(true){
System.out.println(new SimpleDateFormat("mm:ss").format(endTime));
Thread.sleep(1000);
endTime= new Date(endTime.getTime()-1000);
if(end-12000>endTime.getTime()){
break;
}
}
}
public static void test() throws InterruptedException {
//倒数10个数 1s 一个
int num = 10;
while(num!=0){
Thread.sleep(1000);
System.out.println(num--);
}
}
}
4、join合并线程,插队线程
/**
* join合并线程,插队线程
* @author AnQi
* @date 2020/3/7 09 34:56
* @description
*/
public class BlockedJoin01 {
public static void main(String[] args) throws InterruptedException {
Thread t= new Thread(() -> {
for (int i = 0; i < 100; i++) {
System.out.println("lambda" + i);
}
});
t.start();
for (int i = 0; i < 100; i++) {
if (i == 20) {
t.join();//插队 main 被阻塞
}
System.out.println("main" + i);
}
}
}
买烟案例
/**
* join合并线程,插队线程
* @author AnQi
* @date 2020/3/7 09 34:56
* @description
*/
public class BlockedJoin02 {
public static void main(String[] args) throws InterruptedException {
System.out.println("爸爸和儿子买烟的故事");
new Thread(new Fahter()).start();
}
}
class Fahter extends Thread{
@Override
public void run() {
System.out.println("想抽烟发现没了");
System.out.println("让儿子去买中华");
Thread t = new Thread(new son());
t.start();
try {
t.join();//father 被阻塞
System.out.println("老爸接过烟,把零钱给了儿子");
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("孩子丢了 去找孩子。。。");
}
}
}
class son extends Thread{
@Override
public void run() {
System.out.println("拿着爸爸的钱出去了。。。");
System.out.println("路边有个游戏厅玩了10s");
for(int i = 0 ;i<10;i++){
System.out.println(i + "秒过去了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("赶紧去买烟。。。。");
System.out.println("手拿一包中华。。。。");
}
}