java中 sleep线程睡眠 Join线程独占 Yeile 线程让步 Interrupt线程中断 Deamon后台线程

Sleep


package 线程生命周期;

public class TestSleep {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread mythread = new MyThread();
		mythread.start();//子线程,每隔50毫秒输出一行字
		try {
			MyThread.sleep(2000);//这个是暂定的是主线程的
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 1; i < 20; i++) {
			System.out.println("main:"+i);
		}
		
	}

}

//输出1-100的数
class MyThread extends Thread{
	
	public void run(){
		for (int i = 1; i <= 100; i++) {
			System.out.println(i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}


Join

package 线程生命周期;

public class TestJoin {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Mythread3 th1 = new Mythread3();
		th1.setI(20);
		Mythread3 th2 = new Mythread3();
		th2.setI(100);
		/*//交替输出
		th1.start();
		th2.start();*/
		//先执行th1
		th1.start();
		try {
			th1.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		th2.start();
		
	}

}

class Mythread3 extends Thread{

	private int i;
	
	
	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
		for (int j = i; j < i + 50; j++) {
			System.out.println(j);
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}


Yeile 

package 线程生命周期;

public class TestYeile {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建三个优先级不一样的线程 10 5 1的时候都是高的在执行所以全部选5
		new MyThread2("低级",5).start();
		new MyThread2("高级",5).start();
		new MyThread2("中级",5).start();
		
	}

}

//结合线程信息输出1-10
class MyThread2 extends Thread{

	public MyThread2() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	//创建一个构造方法用于给线程命名以及优先级
	public MyThread2(String name,int pro){
		super(name);
		setPriority(pro);
	}
	
	public void run(){
		for (int i = 1; i <= 20; i++) {
			System.out.println(getName()+"线程第"+i+"次运行");
			if(i == 5)
				Thread.yield();//线程让步
		}
	}
}


 Interrupt线程中断

package 线程生命周期;

import java.util.Scanner;

public class TestInterrupt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread4 th4 = new MyThread4();
		th4.start();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("是否中断打印");
		boolean flag  = true;
		if(flag)
			th4.interrupt();
	}

}


//无限次打印i
class MyThread4 extends Thread{
	int i = 0;
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while(true){
			System.out.println(i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				System.out.println("程序执行被中断了");
				return;
				//e.printStackTrace();
			}
			i++;
		}
	}
}


Deamon后台线程

package 线程生命周期;

public class TestDeamon {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread5 th5 = new MyThread5();
		
		MyDeamon myd = new MyDeamon();
		myd.setDaemon(true);
		myd.start();
		th5.start();
		
	}

}

class MyThread5 extends Thread{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		for (int i = 1; i <= 5; i++) {
			
			System.out.println("前台线程第"+i+"次执行");
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

class MyDeamon extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		for (int i = 1; i <= 5; i++) {
			System.out.println("后台线程第"+i+"次执行");
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值