日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java多线程,锁(synchronize),饿汉式单例线程,等待处理机制

發(fā)布時間:2023/12/10 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java多线程,锁(synchronize),饿汉式单例线程,等待处理机制 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一,禮讓和守護(hù)線程

package com.much.hard;public class TestYieldProtect {public static void main(String[] args) {Yield1 y1 = new Yield1();y1.setName("A");Yield2 y2 = new Yield2();y2.setName("B");//y1.start();//y2.start();Daemon1 d1 = new Daemon1();d1.setName("boss");Daemon2 d2 = new Daemon2();d2.setName("bodygGuard");d2.setDaemon(true);d1.start();d2.start();// d2 本來要打印500次的,d1打印10次,可它是守護(hù)線程,老板掛了,它也掛了。// 守護(hù)線程的終止是自身無法控制的,因此千萬不要把IO、File等重要操作邏輯分配給它;因?yàn)樗豢孔V;// GC垃圾回收線程就是很好的例子。/* 當(dāng)我們的程序中不再有任何運(yùn)行的Thread,* 程序就不會再產(chǎn)生垃圾,垃圾回收器也就無事可做,* 所以當(dāng)垃圾回收線程是JVM上僅剩的線程時,垃圾回收線程會自動離開。* 它始終在低級別的狀態(tài)中運(yùn)行,用于實(shí)時監(jiān)控和管理系統(tǒng)中的可回收資源。*/}}class Yield1 extends Thread {public void run() {for (int i = 0; i < 100; i++) {// 暫停當(dāng)前正在執(zhí)行的線程對象,并執(zhí)行其他線程。讓多線程執(zhí)行更和諧,而不是一人一次。if (i == 9) yield();System.out.println(getName() + "\t" + i);}} }class Yield2 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() + "\t" + i);}} }class Daemon1 extends Thread {public void run() {for (int i = 0; i < 10; i++) {System.out.println(getName() + "\t" + i);}} }class Daemon2 extends Thread {public void run() {for (int i = 0; i < 500; i++) {System.out.println(getName() + "\t" + i);}} }

禮讓 當(dāng)A線程是9時,讓B線程執(zhí)行。

二,加入線程

package com.much.hard;public class TestJoin {public static void main(String[] args) {System.out.println("main線程開啟啦");Thread1 t1 = new Thread1();Thread2 t2 = new Thread2();t1.start();t2.start();// 讓1,2線程加入main線程前面 兩個線程開啟... cpu分配交互執(zhí)行。// t1,t2可用來計(jì)算,把結(jié)果反饋到main里面。try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main線程結(jié)束啦");}public static void method() {}}class Thread1 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() +": " + i);}} }class Thread2 extends Thread {public void run() {for (int i = 0; i < 100; i++) {System.out.println(getName() +": " + i);}} }

三,鎖
有了鎖,會讓一個線程結(jié)束后,執(zhí)行下一個線程。。。

package com.much.hard;public class TestSynchronize {public static void main(String[] args) {// 鎖對同一個對象有用 this鎖(同步代碼塊)Print p = new Print();ThreadShow1 ts1 = new ThreadShow1(p);ts1.start();ThreadShow2 ts2 = new ThreadShow2(p);ts2.start();/*Timer t = new Timer();Thread test = new Thread(t);test.start();*/// 里面new對象,和同傳入同一個對象的區(qū)別。要同一個對象作為鎖。// 問題1, abcd 1234 間隔的出現(xiàn)? cpu的資源隨機(jī)分配。// 問題2,用鎖解決,鎖住方法,等這個方法(線程)執(zhí)行完,再執(zhí)行下一個線程。// 問題3,別人講,可以來賣票。。。計(jì)數(shù)。。。}}class Print {public synchronized void printNum() {// 普通同步方法默認(rèn)的鎖時 this關(guān)鍵字System.out.print(1);System.out.print(2);System.out.print(3);System.out.println(4);}public void printLetter() {synchronized(this) {System.out.print("a");System.out.print("b");System.out.print("c");System.out.println("d");}} }class ThreadShow1 extends Thread {// 第一個線程里面打印100條數(shù)字。Print p = null;public ThreadShow1() {}public ThreadShow1(Print p) {this.p = p;}public void run() {//Print p = new Print();for (int i = 0; i < 100; i++) {p.printNum();System.out.println(getName());}} }class ThreadShow2 extends Thread {Print p = null;public ThreadShow2() {}public ThreadShow2(Print p) {this.p = p;}// 第二個線程打印100條字母。public void run() {//Print p = new Print();for (int i = 0; i < 100; i++) {p.printLetter();System.out.println(getName());}} }// 實(shí)現(xiàn)Runnable接口。class Timer implements Runnable{public void run() {System.out.println("10秒計(jì)時,馬上爆炸...");for (int i = 1; i <= 10; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(i);} } }

四,餓漢式單例
鎖機(jī)制的應(yīng)用。

package com.much.hard;public class TestSingle {public static void main(String[] args) {//二:編寫多線程單例模式(懶漢式)//餓漢式,就不用考慮啥了,直接返回,不多bb。for (int i = 0; i < 20; i++) {my1 m = new my1();m.start();}}}class Dog {private static Dog dog = null;private Dog() {}public static Dog show() {if (dog == null) { // 鎖 一起執(zhí)行。 synchronized(Dog.class) {if (dog == null) {// 線程休眠0.1秒。后再執(zhí)行。try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}dog = new Dog();}} }return dog;} }class my1 extends Thread {public void run() {System.out.println(Dog.show()); } }

五,等待處理機(jī)制

package com.much.hard;public class TestWaiting {public static void main(String[] args) {// 等待喚醒機(jī)制。// 需求,我想用線程實(shí)現(xiàn) 1 2 1 2 1 2 1 2... 的循環(huán)。PrintTwo pt = new PrintTwo();Wait1 w1 = new Wait1(pt);Wait2 w2 = new Wait2(pt);w1.start();w2.start();}}class PrintTwo {int i = 1;public synchronized void print1() {if (i != 1) {try {this .wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(1);i = 2;// 喚醒下個線程。// notify是喚醒等待中的一個(隨機(jī)的)獲取對象鎖,進(jìn)入可運(yùn)行狀態(tài),this.notify();}public synchronized void print2() {if (i == 1) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} }System.out.println(2);i = 1;this.notify(); } }class Wait1 extends Thread {PrintTwo pt = null;public Wait1() {}public Wait1(PrintTwo pt) {this.pt = pt;}public void run() {while (true)pt.print1();} }class Wait2 extends Thread {PrintTwo pt = null;public Wait2() {}public Wait2(PrintTwo pt) {this.pt = pt;}public void run() {while (true)pt.print2();} }

哈哈哈,初學(xué)線程,有點(diǎn)點(diǎn)懵,一下子太多了,我只是把老師講的小demo寫了一遍,應(yīng)用還沒有基本。再此做下筆記。

總結(jié)

以上是生活随笔為你收集整理的Java多线程,锁(synchronize),饿汉式单例线程,等待处理机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。