02.第二階段、實戰Java高併發程序設計模式-2.併發基礎

1、併發基礎併發

 什麼是線程
 線程的基本操做
 守護線程
 線程優先級
 基本的線程同步操做
  1. 什麼是線程線程

  2. 線程的基本操做3d

  3. 線程中斷code

    public static native void sleep(long millis) throws InterruptedException
    	public void run(){ while(true){
    	  } }
    	if(Thread.currentThread().isInterrupted()){ System.out.println("Interruted!");
    	break; }
    	try {
    	Thread.sleep(2000);
    	} catch (InterruptedException e) {
    	System.out.println("Interruted When Sleep"); //設置中斷狀態,拋出異常後會清除中斷標記位 Thread.currentThread().interrupt();
    	} Thread.yield();

  4. 守護線程blog

  5. 線程優先級同步

  6. 線程同步it

    public class ThreadTest {
    
    		public static Object object = new Object();
    
    		public static class T2 extends Thread {
    			public void run() {
    				synchronized (object) {
    					System.out.println(System.currentTimeMillis() + ":T2 start! notify one thread");
    					object.notify();
    					System.out.println(System.currentTimeMillis() + ":T2 end!");
    					try {
    						Thread.sleep(2000);
    					} catch (InterruptedException e) {
    					}
    				}
    			}
    		}
    
    		public static class T1 extends Thread {
    			public void run() {
    				synchronized (object) {
    					System.out.println(System.currentTimeMillis() + ":T1 start! ");
    					try {
    						System.out.println(System.currentTimeMillis() + ":T1 wait for object ");
    						object.wait();
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    					System.out.println(System.currentTimeMillis() + ":T1 end!");
    				}
    			}
    		}
    
    		public static void main(String[] args) {
    			T1 t1 = new T1();
    			t1.start();
    			T2 t2 = new T2();
    			t2.start();
    		}
    
    	}
相關文章
相關標籤/搜索