• <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • Java面向對象
    Java異常
    Java數組
    Java常用類
    Java集合
    Java IO流
    Java線程
    Java反射
    Socket編程
    Java注解開發
    Java GoF設計模式
    HashMap
    Java內存模型
    Java線性表

    Java線程調度

     

     

    Java線程優先級

     

    package com.wkcto.chapter07.method;
    /**
     * 線程優先級
     * 	1) 優先級的取值范圍: 1 ~ 10
     * 	2) 所有線程默認的優先級; 5
     * 	3) 優先級越高, 獲得CPU執行權的機率越大
     * 	4) t1.setPriority( 10 )  設置線程優先級
     * @author 蛙課網
     *
     */
    public class Test04 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for(int i = 1; i <= 100; i++){
    					System.out.println( Thread.currentThread().getName() + "-->" + i);
    				}
    			}
    		} , "t1");
    		t1.setPriority(1); 				//設置優先級
    		t1.start();
    		
    		Thread t2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for(int i = 1; i <= 100; i++){
    					System.out.println( Thread.currentThread().getName() + "-->" + i);
    				}
    			}
    		} , "t2");
    		t2.setPriority(10); 		//設置優先級
    		t2.start();
    		
    		//打印線程的優先級
    		System.out.println( "t1 priority: " + t1.getPriority());
    		System.out.println( "t2 priority: " + t2.getPriority());
    		System.out.println( "main priority: " + Thread.currentThread().getPriority());
    		
    		//main線程
    		for(int i = 1; i <= 100; i++){
    			System.out.println( Thread.currentThread().getName() + "-->" + i);
    		}
    	}
    
    }

     

     

    Java線程睡眠

     

    package com.wkcto.chapter07.method;
    /**
     * 線程睡眠 ( 休眠 )
     * 	 Thread.sleep( 2000 );
     * 		1) 是靜態方法, 通過Thread類名直接調用
     * 		2) 睡眠的單位 是毫秒, 1秒 == 1000 毫秒
     * 		3) sleep()有受檢異常需要預處理
     * 		4) sleep()方法所在的線程睡眠 
     * @author 蛙課網
     *
     */
    public class Test05 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for( int i = 1 ; i<=100; i++){
    					System.out.println( Thread.currentThread().getName() + "--> " + i);
    					//當 i == 50 時, 線程休眠
    					if ( i == 50 ) {
    						//run()是重寫了Runnable接口中run(),不能聲明拋出異常,只能捕獲處理
    						try {
    							Thread.sleep(3000);			//睡眠3秒
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    					}
    				}
    			}
    		}, "t1");
    		t1.start();
    		
    //		t1 = null;
    		//main線程
    		for( int i = 1 ; i<=100; i++){
    			System.out.println( Thread.currentThread().getName() + "-----> " + i);
    			//當i==10時, 讓t1線程睡眠 
    			/*try {
    				t1.sleep(3000); 		//雖然是t1調用,實際上是main線程睡眠 
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}*/
    		}
    		
    		System.out.println( t1.getState() );  		//TIMED_WAITING , t1處于sleep()睡眠 狀態
    	}
    
    }

     

     

    Java線程中斷

     

    package com.wkcto.chapter07.method;
    /**
     * 線程中斷
     * 		t1.interrupt();		中斷t1線程
     * 		一般是把處于睡眠 / 等待中的線程給喚醒 
     * 
     * @author 蛙課網
     *
     */
    public class Test06 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for( int i = 1 ; i<=100; i++){
    					System.out.println( Thread.currentThread().getName() + "--> " + i);
    					//當 i == 50 時, 線程休眠
    					if ( i == 50 ) {
    						try {
    							Thread.sleep(10000);			//睡眠10秒
    						} catch (InterruptedException e) {
    //							e.printStackTrace();
    						}
    					}
    				}
    			}
    		}, "t1");
    		t1.start();
    		
    		//main線程
    		for( int i = 1 ; i<=100; i++){
    			System.out.println( Thread.currentThread().getName() + "--> " + i);
    		}
    		
    		//當main線程結束 , 把t1線程喚醒
    		t1.interrupt();  		//中斷t1線程的睡眠, 會拋出中斷異常
    		
    //		System.out.println( t1.isInterrupted() );
    	}
    
    }

     

    package com.wkcto.chapter07.method;
    /**
     * 判斷線程的中斷狀態
     * 		t1.isInterrupted(), 實例方法判斷線程的中斷狀態, 返回true后,不會清除線程的中斷標志
     * 		Thread.interrupted(), 靜態方法判斷線程的中斷狀態, 如果返回true表示線程被中斷了, 然后會清除線程的中斷標志
     * 				再判斷線程的中斷狀態時, 就是false
     * @author 蛙課網
     *
     */
    public class Test07 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    //				如果線程沒有被中斷,就一直打印字符串
    				while( ! Thread.currentThread().isInterrupted() ){
    					System.out.println( "wkcto");
    				}
    				System.out.println("11 : " + Thread.currentThread().isInterrupted()); 
    			}
    		});
    		t1.start();
    		
    		Thread t2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    //				如果線程沒有被中斷,就一直打印字符串
    				while( ! Thread.interrupted() ){
    					System.out.println( "bjpowernode");
    				}
    				System.out.println("22 : " + Thread.currentThread().isInterrupted()); 
    			}
    		});
    		t2.start();
    		
    		// main線程
    		for (int i = 1; i <= 50; i++) {
    			System.out.println(Thread.currentThread().getName() + "--> " + i);
    		}
    		
    		//main線程結束, 就中斷t1線程
    		t1.interrupt();
    		t2.interrupt();
    	}
    
    }

     

     

    Java線程讓步

     

    package com.wkcto.chapter07.method;
    /**
     * 線程讓步
     * 		Thread.yield(); 
     * 		把運行中的線程轉換為就緒狀態
     * @author 蛙課網
     *
     */
    public class Test08 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for (int i = 1; i <= 100; i++) {
    					System.out.println(Thread.currentThread().getName() + "--> " + i);
    					//當 i的值是10的倍數時, 線程讓步
    					if ( i % 10 == 0) {
    						Thread.yield();  	//轉換為就緒狀態
    					}
    				}
    			}
    		} , "t1");
    		t1.start();
    		
    		Thread t2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for (int i = 1; i <= 100; i++) {
    					System.out.println(Thread.currentThread().getName() + "-====> " + i);
    				}
    			}
    		} , "t2");
    		t2.start();
    	}
    
    }

     

     

    Java線程合并

     

    package com.wkcto.chapter07.method;
    /**
     * 線程合并
     * 		t1.join();  	在當前線程中加入t1線程,當前線程轉為等待狀態, 等到t1線程執行完畢后,當前線程再轉為就緒狀態
     * @author 蛙課網
     *
     */
    public class Test09 {
    
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for (int i = 1; i <= 100; i++) {
    					System.out.println(Thread.currentThread().getName() + "--> " + i);
    				}
    			}
    		} , "t1");
    		t1.start();
    		
    		Thread t2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				for (int i = 1; i <= 100; i++) {
    					System.out.println(Thread.currentThread().getName() + "-====> " + i);
    					//當i==10時, 把t1線程合并進來
    					if ( i == 10 ) {
    						try {
    //							t1.join();   	//現在加入t1線程,  當前線程轉為等待狀態, 等到t1執行完后,t2當前線程再轉為就緒狀態
    							t1.join(1000);  //如果當前線程等待1000毫秒后, 不管t1線程是否結束 ,都會轉為就緒狀態
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						} 		
    					}
    				}
    			}
    		} , "t2");
    		t2.start();
    	}
    
    }

     

     

    Java線程終止

     

    package com.wkcto.chapter07.method;
    /**
     * 終止線程
     * 	相辦法讓run()結束 
     * 	可以為線程設計一個布爾標志, 在run()方法中定期判斷這個標志,來決定是否結束 run()
     * @author 蛙課網
     *
     */
    public class Test11 {
    
    	public static void main(String[] args) {
    		SubThread1 thread1 = new SubThread1();
    		thread1.start();
    		
    		Prime3 prime3 = new Prime3();
    		Thread t2 = new Thread(prime3);
    		t2.start();
    		
    		//main線程
    		for (int i = 1; i <= 50; i++) {
    			System.out.println(Thread.currentThread().getName() + "========> " + i);
    		}
    		//main線程結束 , 終止t1線程
    		thread1.stopping = true;
    		prime3.running = false;
    	}
    
    }
    
    class SubThread1  extends Thread{
    	boolean stopping = false;
    	@Override
    	public void run() {
    		for (int i = 1; i <= 500; i++) {
    			if (stopping) {
    				return; 			//結束方法的執行
    			}
    			System.out.println(Thread.currentThread().getName() + "--> " + i);
    		}
    	}
    }
    
    class Prime3 implements Runnable{
    	boolean running = true;
    	@Override
    	public void run() {
    		for( int i = 1; running && i<=500; i++){
    			System.out.println(Thread.currentThread().getName() + "--> " + i);
    		}
    	}
    }

     

    全部教程
  • <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • 面对面棋牌游戏