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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

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

一,禮讓和守護線程

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次,可它是守護線程,老板掛了,它也掛了。// 守護線程的終止是自身無法控制的,因此千萬不要把IO、File等重要操作邏輯分配給它;因為它不靠譜;// GC垃圾回收線程就是很好的例子。/* 當我們的程序中不再有任何運行的Thread,* 程序就不會再產生垃圾,垃圾回收器也就無事可做,* 所以當垃圾回收線程是JVM上僅剩的線程時,垃圾回收線程會自動離開。* 它始終在低級別的狀態中運行,用于實時監控和管理系統中的可回收資源。*/}}class Yield1 extends Thread {public void run() {for (int i = 0; i < 100; i++) {// 暫停當前正在執行的線程對象,并執行其他線程。讓多線程執行更和諧,而不是一人一次。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);}} }

禮讓 當A線程是9時,讓B線程執行。

二,加入線程

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分配交互執行。// t1,t2可用來計算,把結果反饋到main里面。try {t1.join();t2.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main線程結束啦");}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);}} }

三,鎖
有了鎖,會讓一個線程結束后,執行下一個線程。。。

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對象,和同傳入同一個對象的區別。要同一個對象作為鎖。// 問題1, abcd 1234 間隔的出現? cpu的資源隨機分配。// 問題2,用鎖解決,鎖住方法,等這個方法(線程)執行完,再執行下一個線程。// 問題3,別人講,可以來賣票。。。計數。。。}}class Print {public synchronized void printNum() {// 普通同步方法默認的鎖時 this關鍵字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條數字。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());}} }// 實現Runnable接口。class Timer implements Runnable{public void run() {System.out.println("10秒計時,馬上爆炸...");for (int i = 1; i <= 10; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(i);} } }

四,餓漢式單例
鎖機制的應用。

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) { // 鎖 一起執行。 synchronized(Dog.class) {if (dog == null) {// 線程休眠0.1秒。后再執行。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()); } }

五,等待處理機制

package com.much.hard;public class TestWaiting {public static void main(String[] args) {// 等待喚醒機制。// 需求,我想用線程實現 1 2 1 2 1 2 1 2... 的循環。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是喚醒等待中的一個(隨機的)獲取對象鎖,進入可運行狀態,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();} }

哈哈哈,初學線程,有點點懵,一下子太多了,我只是把老師講的小demo寫了一遍,應用還沒有基本。再此做下筆記。

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。