java读者写者_Java 实现读者与写者

这是一个使用Java Swing实现的读者写者问题实验。程序中创建了读取者和写入者,通过Semaphore进行同步控制。当没有写入者时,多个读取者可以同时读取;写入者在写入时会独占资源,确保数据的一致性。用户可以通过点击按钮启动读者线程或写者线程,观察并发行为。
摘要由CSDN通过智能技术生成

1 packager_d;2 /**

3 * 以下读者与写者实验以swing为主要实现4 */

5 import java.awt.*;6 import javax.swing.*;7 importjavax.swing.border.TitledBorder;8

9 import java.awt.event.*;10 importjava.util.Random;11 importjava.util.concurrent.Semaphore;12 importjava.util.concurrent.Executors;13 importjava.util.concurrent.ExecutorService;14 public class Rw2 extends JFrame implementsActionListener{15 /**

16 *17 */

18 private static final long serialVersionUID = 1L;19

20 final Semaphore mutex=new Semaphore(1);21 final Semaphore Rmutex=new Semaphore(5);22 private boolean r=false;23 private int counter=0;24 ExecutorService exc=Executors.newCachedThreadPool();25 private Random ran=newRandom();26 //界面设计控件

27 Container con=getContentPane();28 JLabel bName,blank;29 JLabel []read=new JLabel[5];30 JLabel []w=new JLabel[2];31 JTextArea tf_bContent;32 JTextArea []tf_r=new JTextArea[5];33 JTextArea []tf_w=new JTextArea[2];34 Button b_r,b_w;35

36 publicRw2(){37 con.setLayout(null);38 setTitle("读者和写者问题");39 bName=new JLabel("三国演义");40 bName.setBounds(30, 30, 60, 40);41 tf_bContent=new JTextArea("《三国演义》中国古典四大名著之一");42 tf_bContent.setBounds(30, 60, 300, 80);43 tf_bContent.setBorder(new TitledBorder(null,"",TitledBorder.DEFAULT_JUSTIFICATION,44 TitledBorder.DEFAULT_POSITION,null,Color.blue));45 blank=newJLabel();46 //读者控件初始化

47 for(int i=0;i<5;i++)48 {49

50 read[i]=newJLabel();51 read[i].setText("读者"+String.valueOf(i));52 read[i].setBounds(10, 150+60*i, 60, 50);53

54 tf_r[i]=newJTextArea();55 tf_r[i].setBounds(60, 150+60*i, 100, 50);56 tf_r[i].setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,57 TitledBorder.DEFAULT_POSITION, null, Color.BLUE));58

59 }60 for(int j=0;j<2;j++)61 {62 w[j]=newJLabel();63 w[j].setText("写者"+String.valueOf(j));64 w[j].setBounds(200, 150+60*j, 100, 50);65 tf_w[j]=newJTextArea();66 tf_w[j].setBounds(260, 150+60*j, 100, 50);67 tf_w[j].setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,68 TitledBorder.DEFAULT_POSITION, null, Color.BLUE));69 }70

71 b_r=new Button("启动读者线程");72 b_w=new Button("启动写者线程");73 b_r.setBounds(400,100, 100, 50);74 b_w.setBounds(510,100, 100, 50);75 //安装行动监视器

76 b_r.addActionListener(this);77 b_w.addActionListener(this);78

79

80 con.add(bName);81 con.add(tf_bContent);82 for(int i=0;i<5;i++)83 {84 con.add(tf_r[i]);85 con.add(read[i]);86 }87 for(int j=0;j<2;j++)88 {89

90 con.add(w[j]);91 con.add(tf_w[j]);92 }93 con.add(b_r);94 con.add(b_w);95 setSize(800,500);96 setVisible(true);97 //关闭窗口

98 addWindowListener(newWindowAdapter(){99 public voidwindowClosing(WindowEvent e){100 System.exit(0);101 }102 });103

104 }105

106

107 Runnable run=newRunnable(){108 public voidrun(){109 if(r)110 {111 try{112 Rmutex.acquire();113 if(counter==0)114 {115 try{116 mutex.acquire();117 } catch(InterruptedException e) {118 //TODO Auto-generated catch block

119 e.printStackTrace();120 }121 }122 tf_r[counter].setText(tf_bContent.getText().toString());123 counter++;124 Thread.sleep((long)(Math.random()*10000));125 tf_r[counter-1].setText("");126 if(counter==1)127 mutex.release();128 counter--;129 Rmutex.release();130

131 } catch(InterruptedException e) {132 //TODO Auto-generated catch block133 //e.printStackTrace();

134 }135

136

137

138 }else if(!r)139 {140 try{141 mutex.acquire();142 tf_bContent.setText(tf_w[ran.nextInt(1)].getText().toString());143 Thread.sleep((long)(Math.random()*10000));144 mutex.release();145 } catch(InterruptedException e) {146 //TODO Auto-generated catch block

147 e.printStackTrace();148 }149

150 }151 }152

153 };//run

154 @Override155 public voidactionPerformed(ActionEvent a) {156 //TODO Auto-generated method stub

157 if(a.getSource()==b_r)158 {159 r=true;160 exc.execute(run);161

162 }163 else if(a.getSource()==b_w){164 r=false;165 exc.execute(run);166 }167

168 }169 public static voidmain(String args[])170 {171 newRw2();172 }173 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值