Java_160_Thread_线程五大状态_终止_阻塞sleep_礼让yield_插队join

Java_160_Thread_线程五大状态_终止_阻塞sleep_礼让yield_插队join

2020-06-24 21:31:42发布 浏览数:475
概述:Java_160_Thread_线程五大状态_终止_阻塞sleep_礼让yield_插队join

线程五大状态&终止

//线程状态 五大状态

 * 新生状态

 * 就绪状态-》 运行状态 -》阻塞状态-》就绪状态-》 运行状态 -》阻塞状态...

 * 运行状态

 * 阻塞状态

 * 死亡状态

package TCPUDPThread;



public class statusTest implements Runnable {

	//加入标识 标记线程体是否可以运行

	private boolean flag=true;

	private String name;

	

	public static void main(String[] args) {

		statusTest tt=new statusTest("铁蛋");

		new Thread(tt).start();

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

			if(i==88){

				tt.terminate();

				System.out.println("game over");

			}

			System.out.println("for->main->"+i);

		}

	}

	

	

	public statusTest(String name) {

		super();

		this.name = name;

	}

	

	public statusTest() {

		

	}

	

	public void terminate(){

		this.flag=false;

	}

 

	@Override

	public void run() {

		int i=0;

		//关联标识,true->运行 false->停止

		while(flag){

			System.out.println(name+"-->"+i++);

		}

	}

	

}

阻塞sleep,倒计时模拟

package TCPUDPThread;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 



public class statusTest3 {

	public static void main(String[] args) throws Exception {

		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(endTime.getTime()==(end-10000)){

				break;

			}

		}

	}

	public static void test() throws Exception{

		int num=10;

		while(true){

			Thread.sleep(1000);

			System.out.println(num--);

		}

	}

	

}

礼让yield

package TCPUDPThread;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 



public class statusTest4 implements Runnable{

	public static void main(String[] args) throws Exception {

		statusTest4 tt=new statusTest4();

		new Thread(tt,"T1").start();

		new Thread(tt,"T2").start();

		

		new Thread(()->{

			for(int i=1;i<100;i++){

				System.out.println("lambda..."+i);

			}

		}).start();

		

		for(int i=1;i<100;i++){

			if(i%10==0){

				Thread.yield();

			}

			System.out.println("main..."+i);

		}

	}

 

	@Override

	public void run() {

		System.out.println(Thread.currentThread().getName()+"start");

		Thread.yield();

		System.out.println(Thread.currentThread().getName()+"-end-");

	}

}

插队join

package TCPUDPThread;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 



public class statusTest5 implements Runnable{

	public static void main(String[] args) throws Exception {

		statusTest5 tt=new statusTest5();

		Thread ts=new Thread(tt,"T1-->");

		ts.start();

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

			ts.join();//阻塞main,谁用阻塞谁

			System.out.println("main-->"+i);

		}

	}

 

	@Override

	public void run() {

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

			System.out.println(Thread.currentThread().getName()+i);

		}

	}

}

应用sleep join 练习

package TCPUDPThread;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 



public class statusTest5 {

	public static void main(String[] args) throws Exception {

		System.out.println("爸爸和儿子买烟的故事");

		new Thread(new Father(),"爸爸").start();

	}

}

class Father extends Thread{

	public void run(){

		System.out.println(Thread.currentThread().getName()+":"+"没有了");

		System.out.println(Thread.currentThread().getName()+":"+"儿子去买烟");

		Thread t=new Thread(new Son(),"儿子");

		t.start();

		try {

			t.join();

		} catch (InterruptedException e) {

			e.printStackTrace();

		}

		System.out.println(Thread.currentThread().getName()+":"+"接过烟");

		System.out.println(Thread.currentThread().getName()+":"+"把零钱给了"+t.getName());

	}

}

 

class Son extends Thread{

	public void run(){

		System.out.println(Thread.currentThread().getName()+":"+"接过钱");

		System.out.println(Thread.currentThread().getName()+":"+"去了游戏厅玩了10秒");

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

			System.out.println(Thread.currentThread().getName()+":"+i+"秒过去了");

			try {

				Thread.sleep(1000);

			} catch (InterruptedException e) {

				e.printStackTrace();

			}

		}

		System.out.println(Thread.currentThread().getName()+":"+"赶紧买烟");

		System.out.println(Thread.currentThread().getName()+":"+"买了一包烟");

	}

}

 

请先
登录
后评论
0 条评论
暂时没有评论
最新文章
更多