java扑克发牌程序_Java多线程实现扑克牌发牌程序实例
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class CardBuffer ? ? ? ? ? ? ? ? ? ? ? ? ?//加互斥鎖的緩沖區(qū)
{
private int value;
private boolean isEmpty=true; ? ? ? ? ? ? ? ?//value是否空的信號(hào)量
private int order=0; ? ? ? ? ? ? ? ? ? ? ? ? //信號(hào)量,約定取牌線程的次序
synchronized void put(int i)
{
while (!isEmpty) ? ? ? ? ? ? ? ? ? ? ? ? //當(dāng)value不空時(shí),等待
try
{
this.wait(); ? ? ? ? ? ? ? ? ? ?//等待
}
catch(InterruptedException e) {
}
value=i; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //當(dāng)value空時(shí),value獲得值
isEmpty=false; ? ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置value為不空狀態(tài)
notifyAll(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //喚醒所有其他等待線程
}
synchronized int get(int order) ? ? ? ? ? ? ?//order是取牌線程約定的次序
{
while (isEmpty || (this.order!=order)) ? //當(dāng)value空或取牌次序不符時(shí)等待
try
{
this.wait();
}
catch(InterruptedException e) {
}
isEmpty=true; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//設(shè)置value為空狀態(tài),并返回值
notifyAll();
this.order=(this.order+1)%4; ? ? ? ? ? ? //加1使取牌次序輪轉(zhuǎn)
return value;
}
}
/*
class Sender extends Thread ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//發(fā)牌線程類
{
private CardBuffer cardbuffer;
private int count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //牌數(shù)
public Sender(CardBuffer cardbuffer,int count)
{
this.cardbuffer=cardbuffer;
this.count=count;
}
public void run()
{
for (int i=1; i<=this.count; i++)
cardbuffer.put(i);
}
}*/
class Receiver extends Thread ? ? ? ? ? ? ? ? ? ?//取牌線程類
{
private CardBuffer cardbuffer;
private JTextArea text;
private int order; ? ? ? ? ? ? ? ? ? ? ? ? ? //信號(hào)量,約定取牌線程的次序
public Receiver(CardBuffer cardbuffer,JTextArea text,int order)
{
this.cardbuffer = cardbuffer ;
this.text = text ;
this.order = order;
}
public void run()
{
while(true)
{
text.append(" "+cardbuffer.get(this.order));
try
{
Thread.sleep(100);
}
catch(InterruptedException e) {}
}
}
}
class CardJFrame extends JFrame
{
public CardJFrame()
{
super("發(fā)牌程序");
this.setSize(460,200);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container=this.getContentPane();
container.setLayout(new GridLayout(3,3)); ? ? ? ? ?//3行3列網(wǎng)格布局
container.add(new JPanel()); ? ? ? ? ? ? ? ? ? ? ? //網(wǎng)格布局的第1行第1列
JTextArea text[]=new JTextArea[4]; ? ? ? ? ? ? ? ? //顯示牌號(hào)的4個(gè)文本區(qū)
Font font=new Font("Helvetica", Font.PLAIN, 16);
for (int i=0; i
{
text[i]=new JTextArea();
text[i].setLineWrap(true); ? ? ? ? ? ? ? ? ? ? //設(shè)置文本區(qū)自動(dòng)換行
text[i].setEditable(false);
text[i].setFont(font);
container.add(text[i]);
container.add(new JPanel());
}
this.setVisible(true);
CardBuffer cardbuffer = new CardBuffer();
Sender s = new Sender(cardbuffer,52);
s.setPriority(10); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置最高優(yōu)先級(jí)
s.start(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //啟動(dòng)發(fā)牌線程
for (int i=0; i
(new Receiver(cardbuffer,text[i],i)).start(); ?//創(chuàng)建并啟動(dòng)4個(gè)取牌線程,優(yōu)先級(jí)為5
}
public static void main(String arg[])
{
new CardJFrame();
}
}
//思修改發(fā)牌線程,發(fā)送由1~52組成的一個(gè)隨機(jī)數(shù)序列。
//
class Sender extends Thread ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//發(fā)牌線程類
{
private CardBuffer cardbuffer;
private int count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //牌數(shù)
private java.util.ArrayList list; ? ? ? ? ? ? //數(shù)組列表
public Sender(CardBuffer cardbuffer,int count)
{
this.cardbuffer=cardbuffer;
this.count=count;
list=new ArrayList();
for (int i=1; i<=this.count; i++)
list.add(new Integer(i)); ? ? ? ? ? ? ? ? ? ? ?//列表中添加整數(shù)對(duì)象
java.util.Random rand=new Random(); ? ? ? ? ? ? ? ?//隨機(jī)數(shù)序列對(duì)象
java.util.Collections.shuffle(list, rand); ? ? ? ? //將列表的數(shù)據(jù)序列打散,按隨機(jī)數(shù)序列
}
public void run()
{
// ? ? ? ?for (int i=1; i<=this.count; i++)
// ? ? ? ? ? ?cardbuffer.put(((Integer)list.get(i-1)).intValue());
//或
Iterator it=list.iterator(); ? ? ? ? ? ? ?//返回一個(gè)迭代器對(duì)象
while (it.hasNext()) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //若有后繼元素,使用迭代器遍歷一個(gè)集合
cardbuffer.put((Integer)it.next()); ? ? ? ? ? ?//返回后繼元素
}
}
作者:http://www.loverseo.com
總結(jié)
以上是生活随笔為你收集整理的java扑克发牌程序_Java多线程实现扑克牌发牌程序实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: execl用宏查询mysql_关于EXC
- 下一篇: java apt anno_Androi