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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java基础---多线程同步锁问题

發布時間:2024/2/28 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础---多线程同步锁问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

package com.duoxiancheng;public class Test01_02 {public static void main(String[] args) { // Door d1 = new Door(); // d1.setName("前門"); // Door d2 = new Door(); // d2.setName("中門"); // Door d3 = new Door(); // d3.setName("后門"); // d1.start(); // d2.start(); // d3.start();//創建任務類對象OnBus ob = new OnBus();Thread t1 = new Thread(ob, "前門");Thread t2 = new Thread(ob, "中門");Thread t3 = new Thread(ob, "后門");t1.start();t2.start();t3.start();}}class OnBus implements Runnable {private int seats = 800;@Overridepublic void run() {synchronized (this) {while(true) {if (seats <= 0) {break;}try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}seats--;System.out.println(Thread.currentThread().getName() + "上車---還剩" + seats + "個座...");}}}}

?

?

package com.duoxiancheng;public class Test01_上車 {public static void main(String[] args) {BusDoor door = new BusDoor();Thread t1 = new Thread(door,"前門線程->");Thread t2 = new Thread(door,"中門線程->");Thread t3 = new Thread(door,"后門線程->");t1.start();t2.start();t3.start();}}//設計一個門類實現Runnable class BusDoor implements Runnable{private int count = 800;//之所以設置同步,是為了避免在某個線程休眠時,其他多個線程進入執行,所以總票數減的次數增多。線程不安全public synchronized void run() {while (true) {if (count <= 0) {break;}try {Thread.sleep(10);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}count--;System.out.println(Thread.currentThread().getName() + "上車---還剩" + count + "個座...");}} }

第二個例子:

package com.duoxiancheng;public class Test02_02 {public static void main(String[] args) {printNumber print = new printNumber();Thread t1 = new Thread(print,"線程1->");Thread t2 = new Thread(print,"線程2->");Thread t3 = new Thread(print,"線程3->");t1.start();t2.start();t3.start();} }class printNumber implements Runnable{private int end = 200;private int start = 100;@Overridepublic void run() { while(start <= end) {synchronized (this) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}if (start % 2 == 0) {System.out.println("我是偶數" + start);}else {System.out.println("我是奇數" + start + ",我的名字是:" + Thread.currentThread().getName());}start++;}}} } package com.duoxiancheng;public class Test02 {public static void main(String[] args) {Print print = new Print();Thread t1 = new Thread(print,"線程1->");Thread t2 = new Thread(print,"線程2->");Thread t3 = new Thread(print,"線程3->");t1.start();t2.start();t3.start();} }class Print implements Runnable{@Overridepublic synchronized void run() {for (int i = 100; i <= 200; i++) {if (i % 2 == 0) {System.out.println("輸出偶數"+ i);try {wait();} catch (InterruptedException e) {e.printStackTrace();}}else {System.out.println("輸出奇數" + i +",線程名字:" + Thread.currentThread().getName());notify();}}}}

?

?輸出結果的同步代碼塊時,會出現不同線程搶奪資源。

而同步方法,會使一個線程執行完。

?

?

?

總結

以上是生活随笔為你收集整理的java基础---多线程同步锁问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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