偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Java多線程中Message類和Queue類的使用方法

開發(fā) 后端
Java多線程有很多的分類,首先我們來看看Message類和Queue類之間的具體代碼是什么樣的。只有這樣我們才能更好的進(jìn)行相關(guān)技術(shù)的學(xué)習(xí)。

Java多線程的應(yīng)用中有狠多需要我們主要的事情,其中以Message類和Queue類最為重要。在使用的時候我們需要不斷的學(xué)習(xí)相關(guān)的知識,這樣才能熟練的掌握在Java多線程中相關(guān)的使用方法。

Message類

 

  1. package com.example.test;  
  2. public class Message {  
  3. public static int id;  
  4. public String content;  
  5. public String getContent() {  
  6. return content;  
  7. }  
  8. public void setContent(String content) {  
  9. this.content = content;  
  10. }  
  11. public int getId() {  
  12. return id;  
  13. }  
  14. public void setId(int id) {  
  15. Message.id = id;  
  16. }  

 

Queue類

 

  1. package com.example.test;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. public class Queue {  
  5. List<Message> queue = new ArrayList<Message>();  
  6. /** 隊列中message對象的***值,默認(rèn)為5 */  
  7. int maxMessageNum = 5;  
  8. public synchronized void produce(Message message) {  
  9. this.notifyAll();  
  10. while (queue.size() == maxMessageNum) {  
  11. System.out.println(Thread.currentThread().getName()  
  12. + " 隊列滿!等待中。。。");  
  13. try {  
  14. this.wait();  
  15. } catch (InterruptedException e) {  
  16. e.printStackTrace();  
  17. }  
  18. }  
  19. queue.add(message);  
  20. System.out.println(Thread.currentThread().getName() + "正在生產(chǎn)"  
  21. + message.getContent() + "。。。 ,當(dāng)前個數(shù):" + getCount());  
  22. }  
  23. public synchronized void consume() {  
  24. this.notifyAll();  
  25. while (queue.size() == 0) {  
  26. System.out.println(Thread.currentThread().getName()  
  27. + " 隊列空!等待中。。。");  
  28. try {  
  29. System.out.println("begin!");  
  30. wait();  
  31. System.out.println("end!");  
  32. } catch (InterruptedException e) {  
  33. e.printStackTrace();  
  34. }  
  35. }  
  36. Message message = queue.get(0);  
  37. queue.remove(0);  
  38. System.out.println(Thread.currentThread().getName() + "正在消費"  
  39. + message.getContent() + "。。。 ,當(dāng)前個數(shù): " + getCount());  
  40. }  
  41. public synchronized int getCount() {  
  42. return queue.size(); 

以上就是對Java多線程的詳細(xì)介紹。相關(guān)的問題我們還是會不斷的向大家介紹。

【編輯推薦】

  1. Android Java包各種功能概覽
  2. Scala:Java+函數(shù)式=后函數(shù)式?
  3. C++和C#、Java的區(qū)別集中總結(jié)
  4. Android啟動Java程序應(yīng)用方法詳解
  5. 2010年將是Java模塊化的一年
責(zé)任編輯:張浩 來源: TT網(wǎng)絡(luò)
相關(guān)推薦

2010-03-15 18:18:33

Java多線程

2011-08-08 14:07:49

iPhone開發(fā) 字體

2009-12-02 18:51:12

PHP分頁類

2022-05-27 08:16:37

Thread類Runnable接口

2010-01-06 10:18:02

JSON類

2011-06-24 15:06:40

QT

2009-06-29 18:26:11

Java多線程Synchronize同步類

2009-12-02 14:50:25

PHP接口類inter

2010-07-09 14:39:42

UML類圖

2022-02-21 18:43:42

Spring封裝多線程

2009-06-29 17:54:10

Java多線程Thread類創(chuàng)建線程

2021-09-11 15:26:23

Java多線程線程池

2011-07-21 15:20:31

iPhone SDK 多線程

2009-11-24 15:50:09

PHP上傳類uploa

2009-06-29 18:08:51

Java多線程join方法

2011-06-02 14:51:07

JAVA修飾符

2018-06-20 10:34:56

堆棧iOSswift

2009-06-22 08:39:27

Java常見錯誤Java類

2024-05-21 11:09:17

2009-04-27 13:15:04

多線程方法run()
點贊
收藏

51CTO技術(shù)棧公眾號