JAVA线程中的发牌题

2019-12-02 16:03:16来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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);
    }

}

 


原文链接:https://www.cnblogs.com/chenxiansen/p/11973014.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:5. 彤哥说netty系列之Java NIO核心组件之Channel

下一篇:在Spring Security框架下JWT的实现细节原理