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阻塞时 如 ...
随机推荐
- 模板 - 数据结构 - 线段树/SegmentTree
区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...
- SpringData :下划线的那些破事
今天写一个查询语句的时候,报错如下 org.springframework.beans.factory.BeanCreationException: Error creating bean with ...
- ZR#984
ZR#984 解法: 异或的一个性质: $ a+b \geq a \bigoplus b$ 所以一边读入一边把读进来的值加到答案就行了. #include<iostream> #inclu ...
- Javascript正则RegExp对象replace方法替换url参数值
看别的博客有用eval执行正则表达式的写法, //替换指定传入参数的值,paramName为参数,replaceWith为新值 function replaceParamVal(paramName,r ...
- JAVA的日期类DATE
好记性不如烂笔头. 1:常见场景 字符串转时间格式,日期转换字符串(在前后端交互 json) 导入包(好像我的IDEA 不知道装了什么插件 会自动补齐提示) import java.text.Par ...
- Spring事务原理分析--手写Spring事务
一.基本概念和原理 1.Spring事务 基于AOP环绕通知和异常通知的 2.Spring事务分为编程式事务.声明事务.编程事务包括注解方式和扫包方式(xml) Spring事务底层使用编程事务(自己 ...
- Git - 高级合并
Git - 高级合并https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%AB%98%E7%BA%A7%E5%90%88%E5%B9%B6 ...
- append()、appendChild() 和 innerHTML 的区别
概念和区别:append() 可以同时传入多个节点或字符串,没有返回值: 据说 append 还是试用期的方法,有兼容问题,(但我用了暂时火狐,谷歌,iE都能使用). https://develope ...
- IDEA子项目的相互依赖
A项目依赖B项目 一.B项目打包,执行install 这一步其实就是把你的项目打包到本地仓库,你可以在本地仓库看到相应的jar包 二.在A项目的pom.xml引入依赖可以了 当B项目修改后,需要重新打 ...
- TLS握手协议分析与理解——某HTTPS请求流量包分析
https://xz.aliyun.com/t/1039 HTTPS简介 HTTPS,是一种网络安全传输协议,在HTTP的基础上利用SSL/TLS来对数据包进行加密,以提供对网络服务器的身份认证,保护 ...