📄 proncons.java
字号:
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(labelCons, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(totalConsLabel))))
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelBuff1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelBuff2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelBuff3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelBuff4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelBuff5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(removeConsButton)
.addComponent(removPronButton))
.addGap(93, 93, 93))
);
}// </editor-fold>//GEN-END:initComponents
private void removPronButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removPronButtonActionPerformed
// TODO 将在此处添加您的处理代码:
if(workPron>0)
if(workPron<=pronNum){
pronNum--;
}
workPron--;
if(workPron<=0){
pronNum=0;
labelPron.setText("已停止");
labelPron.setBackground(Color.WHITE);
labelCons.setText("已停止");
labelCons.setBackground(Color.WHITE);
stop=true;
}
totalPronLabel.setText(String.valueOf(workPron));
}//GEN-LAST:event_removPronButtonActionPerformed
private void removeConsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeConsButtonActionPerformed
// TODO 将在此处添加您的处理代码:
if(workCons>0){
if(consNum>=workCons){
consNum--;
}
workCons--;
}
if(workCons<=0){
consNum=0;
labelPron.setText("已停止");
labelPron.setBackground(Color.WHITE);
labelCons.setText("已停止");
labelCons.setBackground(Color.WHITE);
stop=true;
}
totalConsLabel.setText(String.valueOf(workCons));
}//GEN-LAST:event_removeConsButtonActionPerformed
private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
// TODO 将在此处添加您的处理代码:
stop=true;
}//GEN-LAST:event_stopButtonActionPerformed
private void OnAddPronActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OnAddPronActionPerformed
// TODO 将在此处添加您的处理代码:
workPron++;
new Producer(buffer).start();
totalPronLabel.setText(String.valueOf(workPron));
}//GEN-LAST:event_OnAddPronActionPerformed
private void onAddConsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onAddConsActionPerformed
// TODO 将在此处添加您的处理代码:
workCons++;
new Consumer(buffer).start();
totalConsLabel.setText(String.valueOf(workCons));
}//GEN-LAST:event_onAddConsActionPerformed
private void onStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onStartActionPerformed
// TODO 将在此处添加您的处理代码:
stop=false;
if(workPron==0){
workPron=1;
}
if(workCons==0){
workCons=1;
}
for(int i=0;i<workPron;i++)
new Producer(buffer).start();
for(int i=0;i<workCons;i++)
new Consumer(buffer).start();
pronNum=workPron;
consNum=workCons;
totalConsLabel.setText(String.valueOf(workCons));
totalPronLabel.setText(String.valueOf(workPron));
}//GEN-LAST:event_onStartActionPerformed
private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
// TODO 将在此处添加您的处理代码:
}//GEN-LAST:event_formMouseClicked
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton OnAddPron;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel labelBuff1;
private javax.swing.JLabel labelBuff2;
private javax.swing.JLabel labelBuff3;
private javax.swing.JLabel labelBuff4;
private javax.swing.JLabel labelBuff5;
private javax.swing.JLabel labelCons;
private javax.swing.JLabel labelPron;
private javax.swing.JButton onAddCons;
private javax.swing.JButton onStart;
private javax.swing.JButton removPronButton;
private javax.swing.JButton removeConsButton;
private javax.swing.JButton stopButton;
private javax.swing.JLabel totalConsLabel;
private javax.swing.JLabel totalPronLabel;
// 变量声明结束//GEN-END:variables
class Buffer{
private int []contents;
private int num=-1;
private int size;
private boolean available=false;
public Buffer(int size){
this.size=size;
contents=new int[size];
}
public synchronized void get(){
while(num<=0&&!available){
try{
this.wait();
if(consNum>0)
consNum--;
if(consNum<=0){
labelPron.setText("Wait..");
labelPron.setBackground(Color.RED);
}else{
labelPron.setText(String.valueOf(pronNum));
labelPron.setBackground(Color.GREEN);
}
}catch(InterruptedException ie){
}
}
if(pronNum<workPron){
pronNum++;
}
labelPron.setText(String.valueOf(pronNum));
labelPron.setBackground(Color.GREEN);
JLabel tempLabel=arrLab.get(num--);
tempLabel.setBackground(Color.WHITE);
if(num==0)
available=false;
this.notifyAll();
}
public synchronized void put(){
while(num>=4&&available){
try{
if(pronNum>0)
pronNum--;
if(pronNum<=0){
labelCons.setText("Wait..");
labelCons.setBackground(Color.RED);
}
else{
labelCons.setText(String.valueOf(consNum));
labelCons.setBackground(Color.GREEN);
}
this.wait();
} catch(InterruptedException io){
}
}
if(consNum<workCons){
consNum++;
}
labelCons.setText(String.valueOf(consNum));
labelCons.setBackground(Color.GREEN);
arrLab.get(++num).setBackground(Color.YELLOW);
if(num>=this.size-1){
available=true;
}
num=num%this.size;
this.notifyAll();
}
}
class Consumer extends Thread //继承了Thread接口
{
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
while(!stop) {
buffer.get();//调用了get()函数
try {
Thread.sleep((int)(Math.random()*9000+7000));//定义时间
} catch (InterruptedException exc) {
}
}
}
}
//生产者线程
class Producer extends Thread {
private Buffer buffer; //缓冲区
public Producer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
while(!stop) {
buffer.put();
try {
Thread.sleep((int)(Math.random()*8000+4000));//定义时间
} catch (InterruptedException exc) {
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -