发牌题主要考虑的就是线程的问题,一个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线程中的发牌题的更多相关文章

  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. 微信小程序 图片设置为圆形

    要图片圆形显示,需要设置border-radius:50%,还要设置overflow:hidden,具体如下: Tip:user-avatar是图片控件的class .user-avatar { wi ...

  2. 第十六周助教工作总结——NWNU李泓毅

    助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:团队项目设计完善&编码:https://www.cnblogs.com/nwnu-daizh/ ...

  3. 第06组 Alpha冲刺(1/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 宋奕 过去两天完成了哪些任务 主要完成了用户模块接口 具体完成了用户注册登录,忘记.修改密码,修改.查看个人信息 GitHu ...

  4. c#传不确定的参数个数,比如int型

    a(params int[] ) 调用时a(1,2,3,4,5,6)

  5. 降维算法整理--- PCA、KPCA、LDA、MDS、LLE 等

    转自github: https://github.com/heucoder/dimensionality_reduction_alo_codes 网上关于各种降维算法的资料参差不齐,同时大部分不提供源 ...

  6. 关于如何重写Controller和Service技术攻关文档

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

  7. html表格导出Excel的一点经验心得

    最近在做统计功能,要求统计结果(表格)既能查看(BS系统,在浏览器查看),又能输出为excel文件.对于输出excel文件,在网上找到n种方案,因为还需查看,最终选择了统计结果输出为table,查看时 ...

  8. Docs-.NET-C#-指南-语言参考-关键字-值类型-:浮点数值类型

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型-:浮点数值类型 1.返回顶部 1. 浮点数值类型(C# 引用) 2019/10/22 “浮点类型”是“简单类型”的子集,可以使 ...

  9. 一、postman简介

    一.场景 1.开发接口的时候需要快速的调用接口,以便调试 2.测试的时候需要非常方便的调用接口,通过不同的参数去测试接口的输出 3.这些接口调用是需要保存下来的反复运行的 4.在运行过程中如果有断言( ...

  10. 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法

    一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...