📄 current.java
字号:
//package concurrentdemo;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.concurrent.*;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Rectangle;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Current extends JApplet {
boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
Semaphore semCon ;
Semaphore semPro;
RedProduce produceDemo;
BlueProduce consumerDemo;
JButton jButton1 = new JButton();
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Current() {
}
//Initialize the applet
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setForeground(Color.pink);
this.setSize(new Dimension(751, 454));
this.getContentPane().setLayout(null);
jButton1.setBackground(Color.pink);
jButton1.setBounds(new Rectangle(234, 255, 74, 34));
jPanel1.setBounds(new Rectangle(51, 22, 626, 409));
jPanel1.setLayout(null);
this.getContentPane().add(jPanel1, null);
jPanel1.add(jButton1);
jButton1.addActionListener(new Current_jButton1_actionAdapter(this));
jButton1.setText("开始");
semCon=new Semaphore(1) ;
semPro=new Semaphore(0);
produceDemo=new RedProduce(jPanel1.getGraphics() ,semPro,semCon,"A");
consumerDemo=new BlueProduce(jPanel1.getGraphics() ,semPro,semCon,"B");
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
}
public void jButton1_actionPerformed(ActionEvent e) {
produceDemo.start() ;
consumerDemo.start() ;
}
public void jButton2_actionPerformed(ActionEvent e) {
try
{
produceDemo.wait();
consumerDemo.wait() ;
}
catch(InterruptedException t)
{
}
}
public void jButton3_actionPerformed(ActionEvent e) {
produceDemo.notify() ;
consumerDemo.notify() ;
}
}
class Current_jButton1_actionAdapter implements ActionListener {
private Current adaptee;
Current_jButton1_actionAdapter(Current adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Shared
{
public static int count=0;
}
class RedProduce implements Runnable
{
Graphics g;
Semaphore pro;
Semaphore con;
Thread produceDemo=null;
String name;
RedProduce(Graphics g,Semaphore pro,Semaphore con,String na)
{
this.g=g;
this.pro=pro;
this.con=con;
name=na;
}
public void start()
{
if(produceDemo==null)
{
produceDemo=new Thread(this);
produceDemo.start() ;
}
}
public void run()
{
for(int i=0;i<=20;i++)
draw();
}
public void draw()
{
try
{
con.acquire() ;
}
catch(InterruptedException e)
{
System.out.println("d");
}
int num=Shared.count;
int x = (num % 10) * 50;
int y = (num / 10) * 50;
g.setColor(Color.RED);
g.fillRect(x, y, 20, 20);
g.drawString(name + num, x + 20, y + 20);
Shared.count++;
g.drawRect(0,0,550,400);
g.drawString("本程序利用concurrent包中的Semaphoer(),"+" "+"利用该信号量实现多线程间的同步控制",10,300);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.print(e.getStackTrace());
}
pro.release() ;
}
}
class BlueProduce implements Runnable
{
Graphics g;
Semaphore pro;
Semaphore con;
Thread produceDemo=null;
String name;
BlueProduce(Graphics g,Semaphore pro,Semaphore con,String na)
{
this.g=g;
this.pro=pro;
this.con=con;
name=na;
}
public void start()
{
if(produceDemo==null)
{
produceDemo=new Thread(this);
produceDemo.start() ;
}
}
public void run()
{
for(int i=0;i<20;i++)
draw();
}
public void draw()
{
try
{
pro.acquire() ;
}
catch(InterruptedException e)
{
System.out.println("d");
}
int num=Shared.count ;
int x = (num % 10) * 50;
int y = (num / 10) * 50;
g.setColor(Color.BLUE);
g.fillRect(x, y, 20, 20);
g.drawString(name + num, x + 20, y + 20);
Shared.count++;
g.drawRect(0,0,550,400);
g.drawString("本程序利用concurrent包中的Semaphoer(),"+" "+"利用该信号量实现多线程间的同步控制",10,300);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.print(e.getStackTrace());
}
con.release() ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -