发牌题主要考虑的就是线程的问题,一个buffer缓冲区的问题,

首先,发牌的优先级当然是最高的了,但是取牌不能有优先级,否则会一直有牌先取,因此需要一个信号量order,当order=线程的数字时,取get

否则等待,

因此这个只能是线程的基础题吧,我也是刚刚把例题的基本含义搞懂,写下来记录一下

  1. package fapai;
  2.  
  3. public class CardBuffer<T> {
  4. private T obj;
  5. private boolean isEmpty=true;
  6. private int number;
  7. private int order=0;
  8. public CardBuffer(int number)
  9. {
  10. this.number=number;
  11. }
  12. public synchronized void put(T obj)
  13. {
  14. while(!isEmpty)
  15. {
  16. try
  17. {
  18. this.wait();
  19. }
  20. catch(InterruptedException ex){}
  21. }
  22. this.obj=obj;
  23. this.isEmpty=false;
  24. this.notifyAll();
  25. }
  26. public synchronized T get(int order)
  27. {
  28. while(this.isEmpty||this.order!=order)
  29. {
  30. try
  31. {
  32. this.wait();
  33. }
  34. catch(InterruptedException ex){}
  35.  
  36. }
  37. this.isEmpty=true;
  38. this.order=(this.order+1)%this.number;
  39. this.notifyAll();
  40. return this.obj;
  41.  
  42. }
  43. }
  44.  
  45. package fapai;
  46.  
  47. public class CardSendThread extends Thread{
  48. private CardBuffer<Integer> buffer;
  49. private int cardMax,number;
  50. public CardSendThread(CardBuffer<Integer> buffer,int cardMax,int number)
  51. {
  52. this.buffer=buffer;
  53. this.cardMax=cardMax;
  54. this.number=number;
  55. this.setPriority(Thread.MAX_PRIORITY);
  56. }
  57. public void run()
  58. {
  59. for(int i=1;i<=this.cardMax;i++)
  60. this.buffer.put(i);
  61. for(int i=1;i<=this.number;i++)
  62. this.buffer.put(null);
  63. }
  64.  
  65. }
  66.  
  67. package fapai;
  68. import java.awt.*;
  69. import javax.swing.*;
  70. public class CardReceiveJFrame extends JFrame implements Runnable {
  71. private CardBuffer<Integer> buffer;
  72. private JTextArea text;
  73. private int order;
  74. public CardReceiveJFrame(CardBuffer<Integer>buffer,int order,String title,int x,int y)
  75. {
  76. super(title);
  77. this.setBounds(x,y,290,100);
  78. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  79. this.buffer=buffer;
  80. this.order=order;
  81. this.text=new JTextArea();
  82. this.getContentPane().add(this.text);
  83. this.text.setLineWrap(true);
  84. this.text.setEditable(false);
  85. this.text.setFont(new Font("Arial",Font.PLAIN,20));
  86. this.setVisible(true);
  87. new Thread(this).start();
  88.  
  89. }
  90. public void run()
  91. {
  92. while(true)
  93. {
  94. Integer value=this.buffer.get(this.order);
  95. if(value==null)
  96. return ;
  97. this.text.append(String.format("%4d",value));
  98. try
  99. {
  100. Thread.sleep(100);
  101. }
  102. catch(InterruptedException ex){}
  103. }
  104. }
  105.  
  106. }
  107.  
  108. package fapai;
  109.  
  110. public class Deal {
  111. public Deal(int cardMax,int number)
  112. {
  113. CardBuffer<Integer> buffer=new CardBuffer<Integer>(number);
  114. new CardSendThread(buffer,cardMax,number).start();
  115. String titles[]={"北","东","南","西"};
  116. int x[]={400,700,400,100},y[]={200,320,440,320};
  117. for(int i=0;i<number;i++)
  118. new CardReceiveJFrame(buffer,i,titles[i],x[i],y[i]);
  119. }
  120. public static void main(String arg[])
  121. {
  122. new Deal(52,4);
  123. }
  124.  
  125. }

JAVA线程中的发牌题的更多相关文章

  1. java线程中的sleep/wait/notify/yield/interrupt方法 整理

    java线程中的sleep/wait/notify/yield/interrupt方法 sleep 该方法能够使当前线程休眠一段时间 休眠期间,不释放锁 休眠时间结束之后,进入可执行状态,加入到线程就 ...

  2. java线程中的sleep和wait区别

                                                                            面试题:java线程中sleep和wait的区别以及其资 ...

  3. 在Java 线程中返回值的用法

    http://icgemu.iteye.com/blog/467848 在Java 线程中返回值的用法 博客分类: Java Javathread  有时在执行线程中需要在线程中返回一个值:常规中我们 ...

  4. JAVA 线程中的异常捕获

    在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...

  5. Java线程中yield与join方法的区别

    长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...

  6. java线程中的wait和notify以及notifyall

    一.区别与联系 1.1.wait(),notify()和notifyAll()都是java.lang.Object的方法,而确实sleep方法是Thread类中的方法,这是为什么呢?  因为wait和 ...

  7. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  8. Java线程中的同步

    1.对象与锁 每一个Object类及其子类的实例都拥有一个锁.其中,标量类型int,float等不是对象类型,但是标量类型可以通过其包装类来作为锁.单独的成员变量是不能被标明为同步的.锁只能用在使用了 ...

  9. 为什么JAVA线程中没有Running状态?

    面试官问:为什么 Java 线程没有 Running 状态?我懵了 —— 转  芋道源码 什么是 RUNNABLE? 与传统的ready状态的区别 与传统的running状态的区别 当I/O阻塞时 如 ...

随机推荐

  1. C++标准库分析总结(九)——<HashFunction、Tuple>

    一.HashFunction 当我们在使用hash table以及由它做底层的数据结构时,我们必不可少要讨论hash function,所谓的哈希函数就是产生一个数,这个数越乱越好,以至于达到避免碰撞 ...

  2. [Shell]Telnet反弹shell

    原作者:包子love 文章出处:黑白之道 今天给大家介绍两种telnet反弹shell的方法,相对于其他方式反弹shell要简单一点,可作为按需远程控制或留后门使用,希望可以帮助到大家. 前提条件:目 ...

  3. [转] 修改sqlserver的数据库名、物理名称和逻辑文件名

    转载: https://blog.csdn.net/dym0080/article/details/81017777

  4. 小福bbs-冲刺日志(第四天)

    [小福bbs-冲刺日志(第四天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 两个前端完成15个界面 作业的正文 小福bbs-冲刺日志( ...

  5. AUC,ROC我看到的最透彻的讲解

      版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u013385925/article/d ...

  6. vue 遇到防盗链 img显示不出来

    在index.html中添加: <meta name="referrer" content="no-referrer">

  7. 到底啥是鸭子类型(duck typing)带简单例子

    #百度百科鸭子类型定义 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby.Typescript.Perl.Objective-C.Lua.Julia.Jav ...

  8. 写了一个具有future接口的rust测试代码

    写了一个具有future接口的rust测试代码 但没有实现future功能,内部是直接求值 struct Future<T> { t: T, } impl<T> Future& ...

  9. 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 ...

  10. Source Insight 4.0配置格式化工具AStyle.exe

    Source Insight 4.0配置格式化工具AStyle.exe 摘自:https://blog.csdn.net/u012156133/article/details/81566871 1. ...