日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java扑克发牌程序_Java多线程实现扑克牌发牌程序实例

發(fā)布時(shí)間:2024/8/1 java 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java扑克发牌程序_Java多线程实现扑克牌发牌程序实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ā)牌線程類(lèi)

{

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 ? ? ? ? ? ? ? ? ? ?//取牌線程類(lèi)

{

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ā)牌線程類(lèi)

{

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多线程实现扑克牌发牌程序实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。