1 package r_d; 2 /** 3 * 以下读者与写者实验以swing为主要实现 4 */ 5 import java.awt.*; 6 import javax.swing.*; 7 import javax.swing.border.TitledBorder; 8 9 import java.awt.event.*; 10 import java.util.Random; 11 import java.util.concurrent.Semaphore; 12 import java.util.concurrent.Executors; 13 import java.util.concurrent.ExecutorService; 14 public class Rw2 extends JFrame implements ActionListener{ 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=new Random(); 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 public Rw2(){ 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=new JLabel(); 46 //读者控件初始化 47 for(int i=0;i<5;i++) 48 { 49 50 read[i]=new JLabel(); 51 read[i].setText("读者"+String.valueOf(i)); 52 read[i].setBounds(10, 150+60*i, 60, 50); 53 54 tf_r[i]=new JTextArea(); 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]=new JLabel(); 63 w[j].setText("写者"+String.valueOf(j)); 64 w[j].setBounds(200, 150+60*j, 100, 50); 65 tf_w[j]=new JTextArea(); 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(new WindowAdapter(){ 99 public void windowClosing(WindowEvent e){ 100 System.exit(0); 101 } 102 }); 103 104 } 105 106 107 Runnable run=new Runnable(){ 108 public void run(){ 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 block 133 //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 @Override 155 public void actionPerformed(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 void main(String args[]) 170 { 171 new Rw2(); 172 } 173 }