摘要:一創(chuàng)建線(xiàn)程的方式更可以體現(xiàn)面向?qū)ο蟮乃枷耄€(xiàn)程和代碼隔離二定時(shí)器代碼代碼三線(xiàn)程同步通信技術(shù)子線(xiàn)程次,然后主線(xiàn)程次,然后子線(xiàn)程次,然后主線(xiàn)程次。如果有多個(gè)線(xiàn)程同時(shí)到達(dá)點(diǎn)了,那這個(gè)數(shù)據(jù)要寫(xiě)入多次,這是不對(duì)的實(shí)際失去
一、創(chuàng)建線(xiàn)程的方式
package cn.itcast.heima2; public class TraditionalThread { public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); System.out.println(this.getName()); } } }; thread.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); } } }); thread2.start();//更可以體現(xiàn)面向?qū)ο蟮乃枷?,線(xiàn)程和代碼隔離 new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("runnable:"+Thread.currentThread().getName()); } } }){ @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("thread:"+Thread.currentThread().getName()); } } }.start(); } }二、定時(shí)器
代碼1:
package cn.itcast.heima2; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { public static void main(String[] args) { new Timer().schedule(new TimerTask() { @Override public void run() { System.out.println("OUTER:boom!"); } },10000,3000); while(true){ System.out.println(Calendar.getInstance().get(Calendar.SECOND)); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代碼2:
package cn.itcast.heima2; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { static class MyTimerTask extends TimerTask{ static int count=0; @Override public void run() { // TODO Auto-generated method stub count=(count+1)%2; System.out.println("OUTER:boom!"); new Timer().schedule(new MyTimerTask(), 2000+2000*count); } } public static void main(String[] args) { new Timer().schedule(new MyTimerTask(),2000); while(true){ System.out.println(Calendar.getInstance().get(Calendar.SECOND)); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }三、線(xiàn)程同步通信技術(shù)
子線(xiàn)程10次,然后主線(xiàn)程100次,然后子線(xiàn)程10次,然后主線(xiàn)程100次。循環(huán)50次
package cn.itcast.heima2; public class TraditionalThreadComunication { public static void main(String[] args) { // TODO Auto-generated method stub Business business = new TraditionalThreadComunication().new Business(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 50; i++) { business.sub(i); } } }).start(); for (int i = 0; i < 50; i++) { business.main(i); } } class Business { private boolean bShouldSub=true; public synchronized void sub(int i) { if(!bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (int j = 0; j < 10; j++) { System.out.println("sub thread sequence:" + j + " loop of " + i); } bShouldSub=false; this.notify(); } public synchronized void main(int i) { if(bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (int j = 0; j < 100; j++) { System.out.println("main thread sequence:" + j + " loop of " + i); } bShouldSub=true; this.notify(); } } }四、線(xiàn)程范圍內(nèi)共享變量的概念與作用
如下代碼中會(huì)出現(xiàn)問(wèn)題
package cn.itcast.heima2; import java.util.HashMap; import java.util.Random; public class ThreadScopeShareData { private static int data = 0; private static HashMapthreadData = new HashMap<>(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub data = new Random(System.currentTimeMillis()).nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data); } } static class B { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data); } } }
解決方法:
package cn.itcast.heima2; import java.util.HashMap; import java.util.Random; public class ThreadScopeShareData { private static int data = 0; static long[] seed = new long[] { 12345612, 654321 }; private static HashMap五、讀寫(xiě)鎖技術(shù)的妙用threadData = new HashMap<>(); public static void main(String[] args) { new Thread(new ThreadScopeShareData.MyRunnable() { @Override public void run() { // TODO Auto-generated method stub data = MyRunnable.rd.nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); new Thread(new ThreadScopeShareData.MyRunnable() { @Override public void run() { // TODO Auto-generated method stub data = MyRunnable.rd.nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); } static class A { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data); } } static class B { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data); } } abstract static class MyRunnable implements Runnable { static Random rd = new Random(); public abstract void run(); } }
一個(gè)線(xiàn)程在寫(xiě)的時(shí)候其他線(xiàn)程都不能打斷寫(xiě)入過(guò)程,就是寫(xiě)入的時(shí)候不能讀取
package cn.itcast.heima2; import java.util.Random; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class ReadWriteLockTest { static Random rd = new Random(); public static void main(String[] args) { final Queue3 q3 = new Queue3(); for (int i = 0; i < 3; i++) { new Thread() { public void run() { while (true) { q3.get(); } } }.start(); new Thread() { public void run() { while (true) { q3.put(rd.nextInt(10000)); } } }.start(); } } } class Queue3 { private Object data = null;// 共享數(shù)據(jù),只能有一個(gè)線(xiàn)程能寫(xiě)該數(shù)據(jù),但可以有多個(gè)線(xiàn)程同時(shí)讀該數(shù)據(jù)。 ReadWriteLock rwl = new ReentrantReadWriteLock(); public void get() { rwl.readLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to read data!"); Thread.sleep((long) (Math.random() * 1000)); System.out.println(Thread.currentThread().getName() + " have read data :" + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.readLock().unlock(); } } public void put(Object data) { rwl.writeLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to write data!"); Thread.sleep((long) (Math.random() * 1000)); this.data = data; System.out.println(Thread.currentThread().getName() + " have write data: " + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.writeLock().unlock(); } } }
package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class CacheDemo { private Mapcache = new HashMap (); public static void main(String[] args) { // TODO Auto-generated method stub } private ReadWriteLock rwl = new ReentrantReadWriteLock(); public Object getData(String key){ rwl.readLock().lock(); Object value = null; try{ value = cache.get(key); if(value == null){ rwl.readLock().unlock(); rwl.writeLock().lock();//A try{ if(value==null){//如果有多個(gè)線(xiàn)程同時(shí)到達(dá)A點(diǎn)了,那這個(gè)數(shù)據(jù)要寫(xiě)入多次,這是不對(duì)的 value = "aaaa";//實(shí)際失去queryDB(); } }finally{ rwl.writeLock().unlock(); } rwl.readLock().lock(); } }finally{ rwl.readLock().unlock(); } return value; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67508.html
摘要:在中一般來(lái)說(shuō)通過(guò)來(lái)創(chuàng)建所需要的線(xiàn)程池,如高并發(fā)原理初探后端掘金閱前熱身為了更加形象的說(shuō)明同步異步阻塞非阻塞,我們以小明去買(mǎi)奶茶為例。 AbstractQueuedSynchronizer 超詳細(xì)原理解析 - 后端 - 掘金今天我們來(lái)研究學(xué)習(xí)一下AbstractQueuedSynchronizer類(lèi)的相關(guān)原理,java.util.concurrent包中很多類(lèi)都依賴(lài)于這個(gè)類(lèi)所提供的隊(duì)列式...
摘要:在中一般來(lái)說(shuō)通過(guò)來(lái)創(chuàng)建所需要的線(xiàn)程池,如高并發(fā)原理初探后端掘金閱前熱身為了更加形象的說(shuō)明同步異步阻塞非阻塞,我們以小明去買(mǎi)奶茶為例。 AbstractQueuedSynchronizer 超詳細(xì)原理解析 - 后端 - 掘金今天我們來(lái)研究學(xué)習(xí)一下AbstractQueuedSynchronizer類(lèi)的相關(guān)原理,java.util.concurrent包中很多類(lèi)都依賴(lài)于這個(gè)類(lèi)所提供的隊(duì)列式...
摘要:表示的是兩個(gè),當(dāng)其中任意一個(gè)計(jì)算完并發(fā)編程之是線(xiàn)程安全并且高效的,在并發(fā)編程中經(jīng)??梢?jiàn)它的使用,在開(kāi)始分析它的高并發(fā)實(shí)現(xiàn)機(jī)制前,先講講廢話(huà),看看它是如何被引入的。電商秒殺和搶購(gòu),是兩個(gè)比較典型的互聯(lián)網(wǎng)高并發(fā)場(chǎng)景。 干貨:深度剖析分布式搜索引擎設(shè)計(jì) 分布式,高可用,和機(jī)器學(xué)習(xí)一樣,最近幾年被提及得最多的名詞,聽(tīng)名字多牛逼,來(lái),我們一步一步來(lái)?yè)羝魄皟蓚€(gè)名詞,今天我們首先來(lái)說(shuō)說(shuō)分布式。 探究...
摘要:表示的是兩個(gè),當(dāng)其中任意一個(gè)計(jì)算完并發(fā)編程之是線(xiàn)程安全并且高效的,在并發(fā)編程中經(jīng)??梢?jiàn)它的使用,在開(kāi)始分析它的高并發(fā)實(shí)現(xiàn)機(jī)制前,先講講廢話(huà),看看它是如何被引入的。電商秒殺和搶購(gòu),是兩個(gè)比較典型的互聯(lián)網(wǎng)高并發(fā)場(chǎng)景。 干貨:深度剖析分布式搜索引擎設(shè)計(jì) 分布式,高可用,和機(jī)器學(xué)習(xí)一樣,最近幾年被提及得最多的名詞,聽(tīng)名字多牛逼,來(lái),我們一步一步來(lái)?yè)羝魄皟蓚€(gè)名詞,今天我們首先來(lái)說(shuō)說(shuō)分布式。 探究...
摘要:表示的是兩個(gè),當(dāng)其中任意一個(gè)計(jì)算完并發(fā)編程之是線(xiàn)程安全并且高效的,在并發(fā)編程中經(jīng)??梢?jiàn)它的使用,在開(kāi)始分析它的高并發(fā)實(shí)現(xiàn)機(jī)制前,先講講廢話(huà),看看它是如何被引入的。電商秒殺和搶購(gòu),是兩個(gè)比較典型的互聯(lián)網(wǎng)高并發(fā)場(chǎng)景。 干貨:深度剖析分布式搜索引擎設(shè)計(jì) 分布式,高可用,和機(jī)器學(xué)習(xí)一樣,最近幾年被提及得最多的名詞,聽(tīng)名字多牛逼,來(lái),我們一步一步來(lái)?yè)羝魄皟蓚€(gè)名詞,今天我們首先來(lái)說(shuō)說(shuō)分布式。 探究...
閱讀 968·2023-04-25 23:50
閱讀 2002·2021-11-19 09:40
閱讀 613·2019-08-30 13:50
閱讀 2742·2019-08-29 17:11
閱讀 1054·2019-08-29 16:37
閱讀 2998·2019-08-29 12:54
閱讀 2808·2019-08-28 18:17
閱讀 2652·2019-08-26 16:55