在Java多线程的应用中有狠多需要我们主要的事情,其中以Message类和Queue类最为重要。在使用的时候我们需要不断的学习相关的知识,这样才能熟练的掌握在Java多线程中相关的使用方法。

站在用户的角度思考问题,与客户深入沟通,找到平川网站设计与平川网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站建设、企业官网、英文网站、手机端网站、网站推广、国际域名空间、虚拟空间、企业邮箱。业务覆盖平川地区。
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 (); - /** 队列中message对象的***值,默认为5 */
 - int maxMessageNum = 5;
 - public synchronized void produce(Message message) {
 - this.notifyAll();
 - while (queue.size() == maxMessageNum) {
 - System.out.println(Thread.currentThread().getName()
 - + " 队列满!等待中。");
 - try {
 - this.wait();
 - } catch (InterruptedException e) {
 - e.printStackTrace();
 - }
 - }
 - queue.add(message);
 - System.out.println(Thread.currentThread().getName() + "正在生产"
 - + message.getContent() + "。 ,当前个数:" + getCount());
 - }
 - public synchronized void consume() {
 - this.notifyAll();
 - while (queue.size() == 0) {
 - System.out.println(Thread.currentThread().getName()
 - + " 队列空!等待中。");
 - 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() + "正在消费"
 - + message.getContent() + "。 ,当前个数: " + getCount());
 - }
 - public synchronized int getCount() {
 - return queue.size();
 
以上就是对Java多线程的详细介绍。相关的问题我们还是会不断的向大家介绍。
                文章名称:Java多线程中Message类和Queue类的使用方法
                
                文章来源:http://www.csdahua.cn/qtweb/news28/296428.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网