新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java多線程中Message類和Queue類的使用方法
在Java多線程的應(yīng)用中有狠多需要我們主要的事情,其中以Message類和Queue類最為重要。在使用的時(shí)候我們需要不斷的學(xué)習(xí)相關(guān)的知識(shí),這樣才能熟練的掌握在Java多線程中相關(guān)的使用方法。

站在用戶的角度思考問題,與客戶深入溝通,找到平川網(wǎng)站設(shè)計(jì)與平川網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋平川地區(qū)。
Message類
- package com.example.test;
- public class Message {
- public static int id;
- public String content;
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- Message.id = id;
- }
- }
Queue類
- package com.example.test;
- import java.util.ArrayList;
- import java.util.List;
- public class Queue {
- List
queue = new ArrayList (); - /** 隊(duì)列中message對(duì)象的***值,默認(rèn)為5 */
- int maxMessageNum = 5;
- public synchronized void produce(Message message) {
- this.notifyAll();
- while (queue.size() == maxMessageNum) {
- System.out.println(Thread.currentThread().getName()
- + " 隊(duì)列滿!等待中。");
- try {
- this.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- queue.add(message);
- System.out.println(Thread.currentThread().getName() + "正在生產(chǎn)"
- + message.getContent() + "。 ,當(dāng)前個(gè)數(shù):" + getCount());
- }
- public synchronized void consume() {
- this.notifyAll();
- while (queue.size() == 0) {
- System.out.println(Thread.currentThread().getName()
- + " 隊(duì)列空!等待中。");
- try {
- System.out.println("begin!");
- wait();
- System.out.println("end!");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- Message message = queue.get(0);
- queue.remove(0);
- System.out.println(Thread.currentThread().getName() + "正在消費(fèi)"
- + message.getContent() + "。 ,當(dāng)前個(gè)數(shù): " + getCount());
- }
- public synchronized int getCount() {
- return queue.size();
以上就是對(duì)Java多線程的詳細(xì)介紹。相關(guān)的問題我們還是會(huì)不斷的向大家介紹。
分享標(biāo)題:Java多線程中Message類和Queue類的使用方法
文章源于:http://www.dlmjj.cn/article/ccssoje.html


咨詢
建站咨詢
