java必學(xué)必會(huì)之線程(2)
一、線程的優(yōu)先級(jí)別
線程優(yōu)先級(jí)別的使用范例:
package cn.galc.test; public class TestThread6 { public static void main(String args[]) { MyThread4 t4 = new MyThread4(); MyThread5 t5 = new MyThread5(); Thread t1 = new Thread(t4); Thread t2 = new Thread(t5); t1.setPriority(Thread.NORM_PRIORITY + 3);// 使用setPriority()方法設(shè)置線程的優(yōu)先級(jí)別,這里把t1線程的優(yōu)先級(jí)別進(jìn)行設(shè)置 /* * 把線程t1的優(yōu)先級(jí)(priority)在正常優(yōu)先級(jí)(NORM_PRIORITY)的基礎(chǔ)上再提高3級(jí) * 這樣t1的執(zhí)行一次的時(shí)間就會(huì)比t2的多很多 * 默認(rèn)情況下NORM_PRIORITY的值為5 */ t1.start(); t2.start(); System.out.println("t1線程的優(yōu)先級(jí)是:" + t1.getPriority()); // 使用getPriority()方法取得線程的優(yōu)先級(jí)別,打印出t1的優(yōu)先級(jí)別為8 } } class MyThread4 implements Runnable { public void run() { for (int i = 0; i <= 1000; i++) { System.out.println("T1:" + i); } } } class MyThread5 implements Runnable { public void run() { for (int i = 0; i <= 1000; i++) { System.out.println("===============T2:" + i); } } } run()方法一結(jié)束,線程也就結(jié)束了。
二、線程同步
synchronized關(guān)鍵字的使用范例:
package cn.galc.test; public class TestSync implements Runnable { Timer timer = new Timer(); public static void main(String args[]) { TestSync test = new TestSync(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.setName("t1");// 設(shè)置t1線程的名字 t2.setName("t2");// 設(shè)置t2線程的名字 t1.start(); t2.start(); } public void run() { timer.add(Thread.currentThread().getName()); } } class Timer { private static int num = 0; public/* synchronized */void add(String name) {// 在聲明方法時(shí)加入synchronized時(shí)表示在執(zhí)行這個(gè)方法的過程之中當(dāng)前對(duì)象被鎖定 synchronized (this) { /* * 使用synchronized(this)來鎖定當(dāng)前對(duì)象,這樣就不會(huì)再出現(xiàn)兩個(gè)不同的線程同時(shí)訪問同一個(gè)對(duì)象資源的問題了 只有當(dāng)一個(gè)線程訪問結(jié)束后才會(huì)輪到下一個(gè)線程來訪問 */ num++; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name + ":你是第" + num + "個(gè)使用timer的線程"); } } }
線程死鎖的問題:
package cn.galc.test; /*這個(gè)小程序模擬的是線程死鎖的問題*/ public class TestDeadLock implements Runnable { public int flag = 1; static Object o1 = new Object(), o2 = new Object(); public void run() { System.out.println(Thread.currentThread().getName() + "的flag=" + flag); /* * 運(yùn)行程序后發(fā)現(xiàn)程序執(zhí)行到這里打印出flag以后就再也不往下執(zhí)行后面的if語句了 * 程序也就死在了這里,既不往下執(zhí)行也不退出 */ /* 這是flag=1這個(gè)線程 */ if (flag == 1) { synchronized (o1) { /* 使用synchronized關(guān)鍵字把對(duì)象01鎖定了 */ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { /* * 前面已經(jīng)鎖住了對(duì)象o1,只要再能鎖住o2,那么就能執(zhí)行打印出1的操作了 * 可是這里無法鎖定對(duì)象o2,因?yàn)樵诹硗庖粋€(gè)flag=0這個(gè)線程里面已經(jīng)把對(duì)象o1給鎖住了 * 盡管鎖住o2這個(gè)對(duì)象的線程會(huì)每隔500毫秒睡眠一次,可是在睡眠的時(shí)候仍然是鎖住o2不放的 */ System.out.println("1"); } } } /* * 這里的兩個(gè)if語句都將無法執(zhí)行,因?yàn)橐呀?jīng)造成了線程死鎖的問題 * flag=1這個(gè)線程在等待flag=0這個(gè)線程把對(duì)象o2的鎖解開, * 而flag=0這個(gè)線程也在等待flag=1這個(gè)線程把對(duì)象o1的鎖解開 * 然而這兩個(gè)線程都不愿意解開鎖住的對(duì)象,所以就造成了線程死鎖的問題 */ /* 這是flag=0這個(gè)線程 */ if (flag == 0) { synchronized (o2) { /* 這里先使用synchronized鎖住對(duì)象o2 */ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { /* * 前面已經(jīng)鎖住了對(duì)象o2,只要再能鎖住o1,那么就能執(zhí)行打印出0的操作了 可是這里無法鎖定對(duì)象o1,因?yàn)樵诹硗庖粋€(gè)flag=1這個(gè)線程里面已經(jīng)把對(duì)象o1給鎖住了 盡管鎖住o1這個(gè)對(duì)象的線程會(huì)每隔500毫秒睡眠一次,可是在睡眠的時(shí)候仍然是鎖住o1不放的 */ System.out.println("0"); } } } } public static void main(String args[]) { TestDeadLock td1 = new TestDeadLock(); TestDeadLock td2 = new TestDeadLock(); td1.flag = 1; td2.flag = 0; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); t1.setName("線程td1"); t2.setName("線程td2"); t1.start(); t2.start(); } }
解決線程死鎖的問題最好只鎖定一個(gè)對(duì)象,不要同時(shí)鎖定兩個(gè)對(duì)象
生產(chǎn)者消費(fèi)者問題:
package cn.galc.test; /* 范例名稱:生產(chǎn)者--消費(fèi)者問題 * 源文件名稱:ProducerConsumer.java * 要 點(diǎn): * 1. 共享數(shù)據(jù)的不一致性/臨界資源的保護(hù) * 2. Java對(duì)象鎖的概念 * 3. synchronized關(guān)鍵字/wait()及notify()方法 */ public class ProducerConsumer { public static void main(String args[]){ SyncStack stack = new SyncStack(); Runnable p=new Producer(stack); Runnable c = new Consumer(stack); Thread p1 = new Thread(p); Thread c1 = new Thread(c); p1.start(); c1.start(); } } class SyncStack{ //支持多線程同步操作的堆棧的實(shí)現(xiàn) private int index = 0; private char []data = new char[6]; public synchronized void push(char c){ if(index == data.length){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); data[index] = c; index++; } public synchronized char pop(){ if(index ==0){ try{ this.wait(); }catch(InterruptedException e){} } this.notify(); index--; return data[index]; } } class Producer implements Runnable{ SyncStack stack; public Producer(SyncStack s){ stack = s; } public void run(){ for(int i=0; i<20; i++){ char c =(char)(Math.random()*26+'A'); stack.push(c); System.out.println("produced:"+c); try{ Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e){ } } } } class Consumer implements Runnable{ SyncStack stack; public Consumer(SyncStack s){ stack = s; } public void run(){ for(int i=0;i<20;i++){ char c = stack.pop(); System.out.println("消費(fèi):"+c); try{ Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e){ } } } }
以上就是關(guān)于java線程的全部內(nèi)容介紹,大家可以結(jié)合第一篇《java必學(xué)必會(huì)之線程(1)》進(jìn)行學(xué)習(xí),希望可以幫助到大家。
相關(guān)文章
LibrarySystem圖書管理系統(tǒng)開發(fā)(一)
這篇文章主要為大家詳細(xì)介紹了LibrarySystem圖書管理系統(tǒng)開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Java數(shù)組模擬優(yōu)先級(jí)隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)例
這篇文章主要介紹了Java數(shù)組模擬優(yōu)先級(jí)隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)例,優(yōu)先級(jí)隊(duì)列中的元素會(huì)被設(shè)置優(yōu)先權(quán),本文的例子借助了Java中的TreeSet和TreeMap,需要的朋友可以參考下2016-04-04SpringBoot + SpringSecurity 環(huán)境搭建的步驟
這篇文章主要介紹了SpringBoot + SpringSecurity 環(huán)境搭建的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05SpringBoot整合MinIO實(shí)現(xiàn)文件存儲(chǔ)系統(tǒng)的代碼示例
在現(xiàn)代的應(yīng)用程序中,文件存儲(chǔ)和管理是一個(gè)常見的需求,MinIO是一個(gè)開源的對(duì)象存儲(chǔ)系統(tǒng),與Spring?Boot框架結(jié)合使用,可以快速構(gòu)建高性能的文件存儲(chǔ)系統(tǒng),本文將介紹如何使用Spring?Boot和MinIO來實(shí)現(xiàn)文件存儲(chǔ)系統(tǒng)2023-06-06在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟
這篇文章主要給大家介紹了在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟,文中有詳細(xì)的圖文介紹和代碼示例,對(duì)大家的學(xué)習(xí)和工作有一定的幫助,需要的朋友可以參考下2023-09-09SpringBoot?SpringSecurity?JWT實(shí)現(xiàn)系統(tǒng)安全策略詳解
Spring?Security是Spring的一個(gè)核心項(xiàng)目,它是一個(gè)功能強(qiáng)大且高度可定制的認(rèn)證和訪問控制框架。它提供了認(rèn)證和授權(quán)功能以及抵御常見的攻擊,它已經(jīng)成為保護(hù)基于spring的應(yīng)用程序的事實(shí)標(biāo)準(zhǔn)2022-11-11