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