線程常用操作方法(線程命名,線程休眠,線程中斷,線程禮讓,線程優先級)

線程命名

package java1;


import java.util.concurrent.ExecutionException;


class MyThread implements Runnable{
	public void run() {
		System.out.println(Thread.currentThread().getName());
	}
}
public class L1 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		MyThread mt =new MyThread();
		new Thread(mt,"線程A").start();
		new Thread(mt,"線程B").start();
		new Thread(mt).start();
		
	}

}

主線程負責整體流程,子線程負責耗時操作。

線程休眠

package java1;

import java.util.concurrent.ExecutionException;

public class L1 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Runnable run =() -> {
			for(int i=0;i<10;i++) {
				System.out.println(Thread.currentThread().getName()+"i="+i);
				try {
					Thread.sleep(1000);
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		};
		
		new Thread(run,"線程").start();
		
		
		
	}

}

線程中斷

package java1;
public class L1 {

	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(()-> {
			System.out.print("我要睡覺了\n");
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				System.out.println("找死,敢打擾我睡覺!");
				
			}
		});
		
		thread.start();
		Thread.sleep(100);
		if(!Thread.interrupted()) {
			System.out.println("老張,別睡了!");
			thread.interrupt();
		}
		
		
	}

}

線程強制運行

package java1;
public class L1 {

	public static void main(String[] args) throws Exception {
		Thread mainthread=Thread.currentThread();//獲得主線程
		Thread thread = new Thread(()-> {
			for(int i=0;i<50;i++) {
				if(i>3) {//霸道線程來了
					try {
						mainthread.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println("同學"+i+"我要出去玩");
			}
		});
		thread.start();
		for(int i=0;i<50;i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println("霸道同學"+i+"先出去玩");
		}
		
		
	}

}

線程禮讓


public class Two_7 {
	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(()-> {
			for(int i=0;i<50;i++) {	
				if(i%3==0) {
					Thread.yield();
					System.out.println("######");
				}
				System.out.println(Thread.currentThread().getName()+i+"我要出去玩");
			}
		},"普通同學");
		thread.start();
		for(int i=0;i<50;i++) {
			thread.sleep(100);
			System.out.println("霸道同學"+i+"先出去玩");
		}
		
		
	}

}

線程優先級


public class Two_7 {
	public static void main(String[] args) throws Exception {
		Runnable run=()-> {
			for(int i=0;i<10;i++) {	
				System.out.println(Thread.currentThread().getName()+"執行");
			}
		};
		Thread threadA=new Thread(run,"線程A");
		Thread threadB=new Thread(run,"線程B");
		Thread threadC=new Thread(run,"線程C");
		threadA.setPriority(Thread.MIN_PRIORITY);
		threadB.setPriority(Thread.MAX_PRIORITY);
		threadA.setPriority(Thread.NORM_PRIORITY);
		//不一定優先級最高,搶佔資源最快,但是概率提升
		threadA.start();
		threadB.start();
		threadC.start();
		
		
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章