閱讀851 返回首頁    go 技術社區[雲棲]


Java多線程--線程常用操作方法

1、取得和設置線程名稱


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class ThreadNameDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		new Thread(mt).start() ;		// 係統自動設置線程名稱
		new Thread(mt,"線程-A").start() ;		// 手工設置線程名稱
		new Thread(mt,"線程-B").start() ;		// 手工設置線程名稱
		new Thread(mt).start() ;		// 係統自動設置線程名稱
		new Thread(mt).start() ;		// 係統自動設置線程名稱
	}
};

2、取得當前線程 currentThread()

class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class CurrentThreadDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		new Thread(mt,"線程").start() ;		// 啟動線程
		mt.run() ;	// 直接調用run()方法
	}
};

3、判斷線程是否啟動  isAlive()

class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<3;i++){
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class ThreadAliveDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		Thread t = new Thread(mt,"線程");		// 實例化Thread對象
		System.out.println("線程開始執行之前 --> " + t.isAlive()) ;	 // 判斷是否啟動
		t.start() ;	// 啟動線程
		System.out.println("線程開始執行之後 --> " + t.isAlive()) ;	 // 判斷是否啟動
		for(int i=0;i<3;i++){
			System.out.println(" main運行 --> " + i) ;
		}
		// 以下的輸出結果不確定
		System.out.println("代碼執行之後 --> " + t.isAlive()) ;	 // 判斷是否啟動
		
	}
};

4、線程的強製執行 join()


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<50;i++){
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class ThreadJoinDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		Thread t = new Thread(mt,"線程");		// 實例化Thread對象
		t.start() ;	// 啟動線程
		for(int i=0;i<50;i++){
			if(i>10){
				try{
					t.join() ;	// 線程強製運行
				}catch(InterruptedException e){}
			}
			System.out.println("Main線程運行 --> " + i) ;
		}
	}
};

5、線程的休眠   sleep()


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<50;i++){
			try{
					Thread.sleep(500) ;	// 線程休眠
			}catch(InterruptedException e){}
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class ThreadSleepDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		Thread t = new Thread(mt,"線程");		// 實例化Thread對象
		t.start() ;	// 啟動線程
	}
};

6、線程的中斷   interrupt()


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		System.out.println("1、進入run()方法") ;
		try{
				Thread.sleep(10000) ;	// 線程休眠10秒
				System.out.println("2、已經完成了休眠") ;
		}catch(InterruptedException e){
			System.out.println("3、休眠被終止") ;
			return ; // 返回調用處
		}
		System.out.println("4、run()方法正常結束") ;
	}
};
public class ThreadInterruptDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		Thread t = new Thread(mt,"線程");		// 實例化Thread對象
		t.start() ;	// 啟動線程
		try{
				Thread.sleep(2000) ;	// 線程休眠2秒
		}catch(InterruptedException e){
			
		}
		t.interrupt() ;	// 中斷線程執行
	}
};

7、後台線程 setDaemon()



class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		while(true){
			System.out.println(Thread.currentThread().getName() + "在運行。") ;
		}
	}
};
public class ThreadDaemonDemo{
	public static void main(String args[]){
		MyThread mt = new MyThread() ;	// 實例化Runnable子類對象
		Thread t = new Thread(mt,"線程");		// 實例化Thread對象
		t.setDaemon(true) ;	// 此線程在後台運行
		t.start() ;	// 啟動線程
	}
};

8、線程的優先級 setPriority()


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<5;i++){
			try{
					Thread.sleep(500) ;	// 線程休眠
			}catch(InterruptedException e){}
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
		}
	}
};
public class ThreadPriorityDemo{
	public static void main(String args[]){
		Thread t1 = new Thread(new MyThread(),"線程A") ;	// 實例化線程對象
		Thread t2 = new Thread(new MyThread(),"線程B") ;	// 實例化線程對象
		Thread t3 = new Thread(new MyThread(),"線程C") ;	// 實例化線程對象
		t1.setPriority(Thread.MIN_PRIORITY) ;	// 優先級最低
		t2.setPriority(Thread.MAX_PRIORITY) ;	// 優先級最高
		t3.setPriority(Thread.NORM_PRIORITY) ;	// 優先級中等
		t1.start() ;	// 啟動線程
		t2.start() ;	// 啟動線程
		t3.start() ;	// 啟動線程
	}
};
public class MainPriorityDemo{
	public static void main(String args[]){
		System.out.println("主方法的優先級:" + 
			Thread.currentThread().getPriority()) ;	// 取得主方法的優先級
		System.out.println("MAX_PRIORITY = " + Thread.MAX_PRIORITY) ;
		System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY) ;
		System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY) ;
	}
};

9、線程的禮讓 yield()


class MyThread implements Runnable{	// 實現Runnable接口
	public void run(){	// 覆寫run()方法
		for(int i=0;i<5;i++){
			try{
				Thread.sleep(500) ;
			}catch(Exception e){}
			System.out.println(Thread.currentThread().getName()
					+ "運行,i = " + i) ;	// 取得當前線程的名字
			if(i==2){
				System.out.print("線程禮讓:") ;
				Thread.currentThread().yield() ;	// 線程禮讓
			}
		}
	}
};
public class ThreadYieldDemo{
	public static void main(String args[]){
		MyThread my = new MyThread() ;	// 實例化MyThread對象
		Thread t1 = new Thread(my,"線程A") ;
		Thread t2 = new Thread(my,"線程B") ;
		t1.start() ;
		t2.start() ;
	}
};

最後更新:2017-04-03 14:53:40

  上一篇:go 網絡子係統50_ip協議選項填充
  下一篇:go clock_gettime測代碼運行時間