JAVA线程中的发牌题
发牌题主要考虑的就是线程的问题,一个buffer缓冲区的问题,
首先,发牌的优先级当然是最高的了,但是取牌不能有优先级,否则会一直有牌先取,因此需要一个信号量order,当order=线程的数字时,取get
否则等待,
因此这个只能是线程的基础题吧,我也是刚刚把例题的基本含义搞懂,写下来记录一下
- package fapai;
- public class CardBuffer<T> {
- private T obj;
- private boolean isEmpty=true;
- private int number;
- private int order=0;
- public CardBuffer(int number)
- {
- this.number=number;
- }
- public synchronized void put(T obj)
- {
- while(!isEmpty)
- {
- try
- {
- this.wait();
- }
- catch(InterruptedException ex){}
- }
- this.obj=obj;
- this.isEmpty=false;
- this.notifyAll();
- }
- public synchronized T get(int order)
- {
- while(this.isEmpty||this.order!=order)
- {
- try
- {
- this.wait();
- }
- catch(InterruptedException ex){}
- }
- this.isEmpty=true;
- this.order=(this.order+1)%this.number;
- this.notifyAll();
- return this.obj;
- }
- }
- package fapai;
- public class CardSendThread extends Thread{
- private CardBuffer<Integer> buffer;
- private int cardMax,number;
- public CardSendThread(CardBuffer<Integer> buffer,int cardMax,int number)
- {
- this.buffer=buffer;
- this.cardMax=cardMax;
- this.number=number;
- this.setPriority(Thread.MAX_PRIORITY);
- }
- public void run()
- {
- for(int i=1;i<=this.cardMax;i++)
- this.buffer.put(i);
- for(int i=1;i<=this.number;i++)
- this.buffer.put(null);
- }
- }
- package fapai;
- import java.awt.*;
- import javax.swing.*;
- public class CardReceiveJFrame extends JFrame implements Runnable {
- private CardBuffer<Integer> buffer;
- private JTextArea text;
- private int order;
- public CardReceiveJFrame(CardBuffer<Integer>buffer,int order,String title,int x,int y)
- {
- super(title);
- this.setBounds(x,y,290,100);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.buffer=buffer;
- this.order=order;
- this.text=new JTextArea();
- this.getContentPane().add(this.text);
- this.text.setLineWrap(true);
- this.text.setEditable(false);
- this.text.setFont(new Font("Arial",Font.PLAIN,20));
- this.setVisible(true);
- new Thread(this).start();
- }
- public void run()
- {
- while(true)
- {
- Integer value=this.buffer.get(this.order);
- if(value==null)
- return ;
- this.text.append(String.format("%4d",value));
- try
- {
- Thread.sleep(100);
- }
- catch(InterruptedException ex){}
- }
- }
- }
- package fapai;
- public class Deal {
- public Deal(int cardMax,int number)
- {
- CardBuffer<Integer> buffer=new CardBuffer<Integer>(number);
- new CardSendThread(buffer,cardMax,number).start();
- String titles[]={"北","东","南","西"};
- int x[]={400,700,400,100},y[]={200,320,440,320};
- for(int i=0;i<number;i++)
- new CardReceiveJFrame(buffer,i,titles[i],x[i],y[i]);
- }
- public static void main(String arg[])
- {
- new Deal(52,4);
- }
- }
JAVA线程中的发牌题的更多相关文章
- java线程中的sleep/wait/notify/yield/interrupt方法 整理
java线程中的sleep/wait/notify/yield/interrupt方法 sleep 该方法能够使当前线程休眠一段时间 休眠期间,不释放锁 休眠时间结束之后,进入可执行状态,加入到线程就 ...
- java线程中的sleep和wait区别
面试题:java线程中sleep和wait的区别以及其资 ...
- 在Java 线程中返回值的用法
http://icgemu.iteye.com/blog/467848 在Java 线程中返回值的用法 博客分类: Java Javathread 有时在执行线程中需要在线程中返回一个值:常规中我们 ...
- JAVA 线程中的异常捕获
在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...
- Java线程中yield与join方法的区别
长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...
- java线程中的wait和notify以及notifyall
一.区别与联系 1.1.wait(),notify()和notifyAll()都是java.lang.Object的方法,而确实sleep方法是Thread类中的方法,这是为什么呢? 因为wait和 ...
- 模拟做饭系统(java+线程中的join方法)
(一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...
- Java线程中的同步
1.对象与锁 每一个Object类及其子类的实例都拥有一个锁.其中,标量类型int,float等不是对象类型,但是标量类型可以通过其包装类来作为锁.单独的成员变量是不能被标明为同步的.锁只能用在使用了 ...
- 为什么JAVA线程中没有Running状态?
面试官问:为什么 Java 线程没有 Running 状态?我懵了 —— 转 芋道源码 什么是 RUNNABLE? 与传统的ready状态的区别 与传统的running状态的区别 当I/O阻塞时 如 ...
随机推荐
- C++标准库分析总结(九)——<HashFunction、Tuple>
一.HashFunction 当我们在使用hash table以及由它做底层的数据结构时,我们必不可少要讨论hash function,所谓的哈希函数就是产生一个数,这个数越乱越好,以至于达到避免碰撞 ...
- [Shell]Telnet反弹shell
原作者:包子love 文章出处:黑白之道 今天给大家介绍两种telnet反弹shell的方法,相对于其他方式反弹shell要简单一点,可作为按需远程控制或留后门使用,希望可以帮助到大家. 前提条件:目 ...
- [转] 修改sqlserver的数据库名、物理名称和逻辑文件名
转载: https://blog.csdn.net/dym0080/article/details/81017777
- 小福bbs-冲刺日志(第四天)
[小福bbs-冲刺日志(第四天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 两个前端完成15个界面 作业的正文 小福bbs-冲刺日志( ...
- AUC,ROC我看到的最透彻的讲解
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u013385925/article/d ...
- vue 遇到防盗链 img显示不出来
在index.html中添加: <meta name="referrer" content="no-referrer">
- 到底啥是鸭子类型(duck typing)带简单例子
#百度百科鸭子类型定义 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby.Typescript.Perl.Objective-C.Lua.Julia.Jav ...
- 写了一个具有future接口的rust测试代码
写了一个具有future接口的rust测试代码 但没有实现future功能,内部是直接求值 struct Future<T> { t: T, } impl<T> Future& ...
- leetcode 402. Remove K Digits 、321. Create Maximum Number
402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...
- Source Insight 4.0配置格式化工具AStyle.exe
Source Insight 4.0配置格式化工具AStyle.exe 摘自:https://blog.csdn.net/u012156133/article/details/81566871 1. ...