- //package test;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Time;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Random;
- import javax.security.auth.kerberos.KerberosKey;
- import javax.swing.*;
- public class ThreadProcess {
- public static void main(String[] args) {
- ThreadFrame thFrame=new ThreadFrame();
- thFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- thFrame.show();
- //创建线程
- thread1=new Thread1(ThreadPanel.progressBar1);
- thread2=new Thread2(ThreadPanel.progressBar2);
- thread3=new Thread3(ThreadPanel.progressBar3);
- //Thread1.flag=false;
- //Thread2.flag=false;
- //Thread3.flag=false;
- int i=0,j=0;
- int[] array=new int[1000];
- //Random random=new Random(System.currentTimeMillis());
- for(i=0;i<1000;i++)
- {
- j=(int) (Math.random()*10%3);
- array[i]=j;
- }
- while(true)
- {
- for(i=0;i<1000;i++)
- {
- switch (array[i]) {
- case 0:
- if (thread1.flag==true) {
- thread1.run();
- }
- else {
- thread1.yield();
- }
- break;
- case 1:
- if(thread2.flag==true){
- thread2.run();
- }
- else {
- thread2.yield();
- }
- break;
- case 2:
- if(thread3.flag==true){
- thread3.run();
- }
- else {
- thread3.yield();
- }
- break;
- }
- }
- }
- }
- public static Thread1 thread1;
- public static Thread2 thread2;
- public static Thread3 thread3;
- }
- class ThreadFrame extends JFrame
- {
- public int WIDTH=820;
- public int HEIGHT=500;
- public ThreadPanel threadPanel=new ThreadPanel();
- public Container container;
- public ThreadFrame()
- {
- setTitle("模拟进程并发");
- setSize(WIDTH,HEIGHT);
- setLocation(250, 150);
- container=getContentPane();
- container.add(threadPanel);
- }
- }
- class ThreadPanel extends JPanel
- {
- public static JProgressBar progressBar1;
- public static JProgressBar progressBar2;
- public static JProgressBar progressBar3;
- JButton buttonstart1;
- JButton buttonstop1;
- JButton buttonstart2;
- JButton buttonstop2;
- JButton buttonstart3;
- JButton buttonstop3;
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- Font f = new Font("宋体",Font.BOLD+Font.ITALIC,15);
- g.setFont(f);
- g.drawString("进程一:",20,90);
- g.drawString("进程二:",20,210);
- g.drawString("进程三:",20,320);
- }
- public ThreadPanel()
- {
- setLayout(null);
- setSize(800, 400);
- progressBar1=new JProgressBar();
- progressBar2=new JProgressBar();
- progressBar3=new JProgressBar();
- buttonstart1=new JButton("开始");
- buttonstop1=new JButton("暂停");
- buttonstart2=new JButton("开始");
- buttonstop2=new JButton("暂停");
- buttonstart3=new JButton("开始");
- buttonstop3=new JButton("暂停");
- progressBar1.setBackground(new Color(255,50,255) );
- progressBar1.setForeground(new Color(10,90,90));
- progressBar1.setBounds(100, 70, 480, 40);
- buttonstart1.setBounds(600, 70, 80, 40);
- buttonstop1.setBounds(700, 70, 80, 40);
- add(progressBar1);
- add(buttonstart1);
- add(buttonstop1);
- buttonstart1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread1.flag=true;
- }
- });
- buttonstop1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread1.flag=false;
- }
- });
- progressBar2.setBackground(new Color(255,90,100) );
- progressBar2.setForeground(new Color(90,50,90));
- progressBar2.setBounds(100, 190, 480, 40);
- buttonstart2.setBounds(600, 190, 80, 40);
- buttonstop2.setBounds(700, 190, 80, 40);
- add(progressBar2);
- add(buttonstart2);
- add(buttonstop2);
- buttonstart2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread2.flag=true;
- }
- });
- buttonstop2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread2.flag=false;
- }
- });
- progressBar3.setBackground(new Color(100,100,255) );
- progressBar3.setForeground(new Color(100,90,90));
- progressBar3.setBounds(100, 300, 480,40);
- buttonstart3.setBounds(600, 300, 80, 40);
- buttonstop3.setBounds(700, 300, 80, 40);
- add(progressBar3);
- add(buttonstart3);
- add(buttonstop3);
- buttonstart3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread3.flag=true;
- }
- });
- buttonstop3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- Thread3.flag=false;
- }
- });
- }
- }
- class Thread1 extends Thread
- {
- public static boolean flag=true;
- JProgressBar progressBar;
- private static int i=0;
- public Thread1(JProgressBar progressBar)
- {
- this.progressBar=progressBar;
- }
- @Override
- public void run() {
- if (i<=100) {
- i+=1;
- progressBar.setValue(i);
- try {
- Thread.sleep(30);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- else {
- i=0;
- }
- }
- }
- class Thread2 extends Thread
- {
- public static boolean flag=true;
- JProgressBar progressBar;
- private static int i=0;
- public Thread2(JProgressBar progressBar)
- {
- this.progressBar=progressBar;
- }
- @Override
- public void run() {
- if (i<=100) {
- i+=1;
- progressBar.setValue(i);
- try {
- Thread.currentThread().sleep(30);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- else {
- i=0;
- }
- }
- }
- class Thread3 extends Thread
- {
- public static boolean flag=true;
- JProgressBar progressBar;
- private static int i=0;
- public Thread3(JProgressBar progressBar)
- {
- this.progressBar=progressBar;
- }
- @Override
- public void run() {
- if (i<=100) {
- i+=1;
- progressBar.setValue(i);
- try {
- Thread.currentThread().sleep(30);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- else {
- i=0;
- }
- }
- }
复制代码
|