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阻塞时 如 ...
随机推荐
- [luogu 3803]【模板】多项式乘法
传送门 FFT #include<bits/stdc++.h> #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #de ...
- CF1217题解
E 也不知道为啥这题咕了好久~ 有一个明显的结论:如果存在有一位有两个数该为不为0,则这两个数可以组成一个满足条件的解 每一位分别维护不为0的和最小的即可
- SpringBoot 配置Druid:不显示SQL监控 —(*) property for user to setup
题外话: SpringBoot整合Druid 请查看https://www.cnblogs.com/JealousGirl/p/druid.html Druid登录后数据源页面.SQL监控等不显示数据 ...
- pymongo helper
import pymongo import click # 数据库基本信息 db_configs = { 'type': 'mongo', 'host': '127.0.0.1', 'port': ' ...
- kafka集群搭建(图文并用)
将安装包上传服务器并解压 scp kafka_2.11-1.0.0.tgz username@{ip}:~/. mkdir /usr/local/kafka mv kafka_2.11-1.0.0.t ...
- Flutter -------- Http库实现网络请求
第三方库 http实现网络请求,包含get,post http库文档:https://pub.dev/packages/http 1.添加依赖 dependencies: http: ^0.12.0 ...
- oracle连接-会话-进程
ALTER SYSTEM SET RESOURCE_LIMIT=TRUE;CREATE PROFILE kyc_pro LIMIT IDLE_TIME 2;alter user kyc_acc pro ...
- 安卓终端-Termux
Termux是一个 Android 终端模拟器以及提供 Linux 环境的应用程序.跟许多其他应用程序不同,无需 root 设备也无需进行设置.它是开箱即用的!它会自动安装好一个最基本的 Linux ...
- Windows下MariaDB数据库安装图文教程
MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQL数据库可以迁移到MariaDB中使用 说明: MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQ ...
- 转 检查rac服务时,发现ons服务offline
检查rac服务时,发现ons服务offline 时间:2014-12-23 11:17:37 作者:solgle 来源:www.solgle.com 查看:4075 评论:0 ...