void setPriority();
涉及優(yōu)先級(jí)的demo(線程優(yōu)先級(jí)越高,分配的時(shí)間片相對(duì)越長(zhǎng)):
public class TestPriority { public static void main(String[] args) { Runnable runnable1=new T1(); Runnable runnable2=new T2(); Thread thread1=new Thread(runnable1); Thread thread2=new Thread(runnable2); thread1.setPriority(Thread.NORM_PRIORITY+3); thread1.start(); thread2.start(); }}class T1 implements Runnable{ public void run() { for(int i=0;i<1000;i++){ System.out.println("T1: "+i); } } }class T2 implements Runnable{ public void run() { for(int i=0;i<1000;i++){ System.out.println("T2: "+i); } } }4.1 線程狀態(tài)控制4.1.1 sleep()方法 (讓當(dāng)前線程休眠)(如何優(yōu)雅地關(guān)掉一個(gè)線程 interrupt、stop、通過標(biāo)志位【推薦】)
import java.util.Date;/*sleep用法*/public class TestInterrupted { public static void main(String[] args) { Runnable thread = new Mythread1();// 創(chuàng)建線程 Thread mythread=new Thread(thread); mythread.start();// 使線程處于就緒狀態(tài) try { Thread.sleep(10000); } catch (InterruptedException e) { } mythread.interrupt(); }}/* * 推薦使用--通過實(shí)現(xiàn)接口來實(shí)現(xiàn)Thread的創(chuàng)建 * 1.1 java里面只有單繼承,這樣線程類的靈活性更好 */class Mythread1 implements Runnable{ public void run() { while(true) { System.out.println("---" + new Date() + "---"); try { Thread.sleep(1000); } catch (InterruptedException e) { // 捕捉到異常時(shí)退出 return; } } }} **如何優(yōu)雅關(guān)閉一個(gè)線程:
public class TestThread1 { public static void main(String[] args) { Runner1 runner1=new Runner1(); Thread thread=new Thread(runner1); thread.start(); for(int i=0;i<100000;i++){ if(i%10000==0&&i>0){ System.out.println("in main thread i="+i); } } System.out.println("thread main is over"); //thread.stop(); //不推薦使用stop() runner1.shutDown(); }}class Runner1 implements Runnable{ //如何地優(yōu)雅關(guān)閉某個(gè)線程:實(shí)質(zhì)就是優(yōu)雅停止執(zhí)行線程的方法體:run() private boolean flag=true; public void run() { int i=0; while(flag==true){ System.out.println("i: "+i++); } } public void shutDown(){ flag=false; } }4.1.2 join()方法 (合并某個(gè)線程)
public class TestJoin { public static void main(String[] args) { Runnable runnable=new Mythread2(); Thread thread=new Thread(runnable); thread.setName("thread1"); thread.start(); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=1;i<=10;i++){ System.out.println("i am main thread"); } } }class Mythread2 implements Runnable{ public void run() { for(int i=1;i<=10;i++){ System.out.println("i am "+Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }4.1.3 yield()方法 (讓出cpu,給其他線程執(zhí)行的機(jī)會(huì))
public class TestYield { public static void main(String[] args) { Runnable runnable=new MyThread3(); Thread thread1=new Thread(runnable); Thread thread2=new Thread(runnable); thread1.start(); thread2.start(); for(int i=0;i<20;i++){ System.out.println("mainThread"+": "+i); if(i%10==0){ Thread.yield(); } } }}class MyThread3 implements Runnable{ @Override public void run() { for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().getName()+": "+i); if(i%10==0){ Thread.yield(); } } } }4.1.4 wait()方法 、notify()/notifyAll()方法(配對(duì)使用、前提:獲得鎖)
wait():

notify():

經(jīng)典的消費(fèi)者生產(chǎn)者問題:
public class ProductComsumer { public static void main(String[] args) { SyncStack stack=new SyncStack(); Producter producter=new Producter(stack); Comsumer comsumer=new Comsumer(stack); new Thread(producter).start(); new Thread(comsumer).start();}}//饅頭類class WoTou{ private int id; public WoTou(int id){ this.id=id; } public String toString(){ return "WoTou : "+id; }}//蒸籠類class SyncStack{ private int index=0; private WoTou[] arrWT=new WoTou[6]; //生產(chǎn)饅頭 public synchronized void push(WoTou wt){ while(index==arrWT.length){ try { this.wait(); //停止生產(chǎn)饅頭 } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); //通知消費(fèi)饅頭 arrWT[index]=wt; index++; } //消費(fèi)饅頭 public synchronized WoTou pop(){ while(index==0){ try { this.wait(); //停止消費(fèi)饅頭 } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); //通知生產(chǎn)饅頭 index--; return arrWT[index]; }}//生產(chǎn)者類class Producter implements Runnable{ private SyncStack stack=null; public Producter(SyncStack stack){ this.stack=stack; } public void run() { WoTou woTou=null; for(int i=0;i<20;i++){ woTou=new WoTou(i); stack.push(woTou); System.out.println("生產(chǎn)了:"+woTou); try { Thread.sleep((int)Math.random()*1000); } catch (InterruptedException e){ e.printStackTrace(); } } } }//消費(fèi)者類class Comsumer implements Runnable{ private SyncStack stack=null; public Comsumer(SyncStack stack){ this.stack=stack; } public void run() { WoTou woTou=null; for(int i=0;i<20;i++){ woTou=stack.pop(); System.out.println("消費(fèi)了:"+woTou); try { Thread.sleep((int)Math.random()*1000); } catch (InterruptedException e){ e.printStackTrace(); } } } }5.1 線程同步 5.1.1 當(dāng)用synchronized來修飾一個(gè)方法或代碼塊的時(shí)候,能夠保證在同一時(shí)刻最多只能只有 一個(gè)線程執(zhí)行該段代碼 5.1.2 當(dāng)兩個(gè)并發(fā)線程訪問同一對(duì)象object中的這個(gè)synchronized(this)同步代碼塊時(shí),一個(gè)時(shí)間內(nèi) 只能有一個(gè)線程得到執(zhí)行。另外一個(gè)線程必須等待當(dāng)前線程執(zhí)行完這個(gè)代碼塊以后才能執(zhí)行該代碼塊 5.1.3 當(dāng)一個(gè)線程訪問object的一個(gè)synchronized(this)同步代碼塊時(shí),另一個(gè)線程仍然可以訪問該 object的非synchronized(this)同步代碼塊 5.1.4 *當(dāng)一個(gè)線程訪問object的一個(gè)synchronize(this)代碼塊,其他線程對(duì)該object中所有其他 synchronized(this)同步代碼塊的訪問將被阻塞 5.1.5 synchronized同步方法、synchronized同步方法塊 5.1.6 死鎖就是指多個(gè)進(jìn)程(線程)因競(jìng)爭(zhēng)資源(系統(tǒng)資源競(jìng)爭(zhēng)、程序推進(jìn)順序非法)而造成的一種僵局 5.1.7 死鎖產(chǎn)生的必要條件:互斥條件、不可剝奪條件、請(qǐng)求和保持條件、循環(huán)等待鏈條件
線程同步demo:
public class TestSync implements Runnable { Timer timer=new Timer(); public static void main(String[] args) { TestSync test=new TestSync(); Thread thread1=new Thread(test); Thread thread2=new Thread(test); thread1.setName("t1"); thread2.setName("t2"); thread1.start(); thread2.start(); } public void run() { timer.add(Thread.currentThread().getName()); }}class Timer { private static int num = 0; public void add(String name) { synchronized (this) { num++; try { Thread.sleep(1); } catch (InterruptedException e) { } System.out.println(name + ",你是第" + num + "個(gè)使用timer的線程"); } }} 線程死鎖:public class DeadLock { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); Thread t1 = new T1(o1,o2); Thread t2 = new T2(o1,o2); t1.start(); t2.start(); }}class T1 extends Thread{ Object o1; Object o2; T1(Object o1,Object o2){ this.o1 = o1; this.o2 = o2; } public void run() { synchronized (o1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { } } }}class T2 extends Thread{ Object o1; Object o2; T2(Object o1,Object o2){ this.o1 = o1; this.o2 = o2; } public void run() { synchronized (o2) { synchronized (o1) { } } }} 經(jīng)典多線程面試題public class TT implements Runnable{ private int b=100; public synchronized void m1() throws Exception{ b=1000; Thread.sleep(5000); System.out.println("b = "+b); }// 1. public void m2() throws Exception{ Thread.sleep(2500); b=2000; } // 2.// public synchronized void m2() throws Exception{// Thread.sleep(2500);// b=2000;// } public void run() { try { m1(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception{ TT tt=new TT(); Thread t=new Thread(tt); t.start(); tt.m2(); System.out.println(tt.b); } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注