A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hhh233 中级黑马   /  2015-3-27 21:58  /  390 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //package test;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Time;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.Random;
  9. import javax.security.auth.kerberos.KerberosKey;
  10. import javax.swing.*;
  11. public class ThreadProcess {
  12. public static void main(String[] args) {
  13.                 ThreadFrame thFrame=new ThreadFrame();
  14.                 thFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.                 thFrame.show();
  16. //创建线程
  17.         thread1=new Thread1(ThreadPanel.progressBar1);
  18.         thread2=new Thread2(ThreadPanel.progressBar2);
  19.         thread3=new Thread3(ThreadPanel.progressBar3);
  20.                 //Thread1.flag=false;
  21.                 //Thread2.flag=false;
  22.                 //Thread3.flag=false;
  23.                 int i=0,j=0;
  24.                 int[] array=new int[1000];
  25. //Random random=new Random(System.currentTimeMillis());
  26. for(i=0;i<1000;i++)
  27.                 {
  28.                   j=(int) (Math.random()*10%3);
  29.                   array[i]=j;
  30.                 }
  31. while(true)
  32.                 {
  33.                    for(i=0;i<1000;i++)
  34.                    {
  35.                           switch (array[i]) {
  36.                                    case 0:
  37.                                            if (thread1.flag==true) {
  38.                                                 thread1.run();
  39.                                         }
  40.                                            else {
  41.                                                 thread1.yield();
  42.                                         }
  43.                                            break;
  44.                                    case 1:
  45.                                            if(thread2.flag==true){
  46.                                                    thread2.run();
  47.                                            }
  48.                                            else {
  49.                                                 thread2.yield();
  50.                                         }
  51.                                            break;
  52.                                    case 2:
  53.                                            if(thread3.flag==true){
  54.                                                    thread3.run();
  55.                                            }
  56.                                            else {
  57.                                                 thread3.yield();
  58.                                         }
  59.                                            break;
  60.                           }
  61.                    }
  62.                 }
  63.         }
  64.         public static Thread1 thread1;
  65.         public static Thread2 thread2;
  66.         public static Thread3 thread3;
  67. }
  68. class ThreadFrame extends JFrame
  69. {
  70.         public int WIDTH=820;
  71.         public int HEIGHT=500;
  72.         public ThreadPanel threadPanel=new ThreadPanel();
  73.         public Container container;
  74. public ThreadFrame()
  75.         {
  76.                 setTitle("模拟进程并发");
  77.                 setSize(WIDTH,HEIGHT);
  78.                 setLocation(250, 150);
  79.                 container=getContentPane();
  80.             container.add(threadPanel);
  81.         }
  82. }
  83. class ThreadPanel extends JPanel
  84. {
  85.         public static JProgressBar progressBar1;
  86.         public static JProgressBar progressBar2;
  87.         public static JProgressBar progressBar3;
  88.         JButton buttonstart1;
  89.         JButton buttonstop1;
  90.         JButton buttonstart2;
  91.         JButton buttonstop2;
  92.         JButton buttonstart3;
  93.         JButton buttonstop3;
  94.         public void paintComponent(Graphics g)
  95.         {
  96.                 super.paintComponent(g);
  97.                 Font f = new Font("宋体",Font.BOLD+Font.ITALIC,15);
  98.                 g.setFont(f);
  99.         g.drawString("进程一:",20,90);
  100.         g.drawString("进程二:",20,210);
  101.         g.drawString("进程三:",20,320);
  102.         }
  103.         public ThreadPanel()
  104.         {
  105.                 setLayout(null);
  106.                 setSize(800, 400);
  107.                 progressBar1=new JProgressBar();
  108.                 progressBar2=new JProgressBar();
  109.                 progressBar3=new JProgressBar();
  110.                 buttonstart1=new JButton("开始");
  111.                 buttonstop1=new JButton("暂停");
  112.                 buttonstart2=new JButton("开始");
  113.                 buttonstop2=new JButton("暂停");
  114.                 buttonstart3=new JButton("开始");
  115.                 buttonstop3=new JButton("暂停");
  116.                 progressBar1.setBackground(new Color(255,50,255) );
  117.                 progressBar1.setForeground(new Color(10,90,90));
  118.                 progressBar1.setBounds(100, 70, 480, 40);
  119.                 buttonstart1.setBounds(600, 70, 80, 40);
  120.                 buttonstop1.setBounds(700, 70, 80, 40);
  121.                 add(progressBar1);
  122.                 add(buttonstart1);
  123.                 add(buttonstop1);
  124.                 buttonstart1.addActionListener(new ActionListener() {
  125.                         @Override
  126.                         public void actionPerformed(ActionEvent arg0) {

  127.                                 Thread1.flag=true;
  128.                         }
  129.                 });
  130.                 buttonstop1.addActionListener(new ActionListener() {
  131.                         @Override
  132.                         public void actionPerformed(ActionEvent arg0) {
  133.                                 Thread1.flag=false;
  134. }
  135.                 });
  136.                 progressBar2.setBackground(new Color(255,90,100) );
  137.                 progressBar2.setForeground(new Color(90,50,90));
  138.                 progressBar2.setBounds(100, 190, 480, 40);
  139.                 buttonstart2.setBounds(600, 190, 80, 40);
  140.                 buttonstop2.setBounds(700, 190, 80, 40);
  141.                 add(progressBar2);
  142.                 add(buttonstart2);
  143.                 add(buttonstop2);
  144.         buttonstart2.addActionListener(new ActionListener() {
  145.         @Override
  146.                         public void actionPerformed(ActionEvent arg0) {
  147. Thread2.flag=true;
  148.                         }
  149.                 });
  150.                 buttonstop2.addActionListener(new ActionListener() {

  151.                         @Override
  152.                         public void actionPerformed(ActionEvent arg0) {

  153.                                 Thread2.flag=false;
  154.                         }
  155.                 });
  156.                 progressBar3.setBackground(new Color(100,100,255) );
  157.                 progressBar3.setForeground(new Color(100,90,90));
  158.                 progressBar3.setBounds(100, 300, 480,40);
  159.                 buttonstart3.setBounds(600, 300, 80, 40);
  160.                 buttonstop3.setBounds(700, 300, 80, 40);
  161.                 add(progressBar3);
  162.                 add(buttonstart3);
  163.                 add(buttonstop3);

  164.                 buttonstart3.addActionListener(new ActionListener() {

  165.                         @Override
  166.                         public void actionPerformed(ActionEvent arg0) {

  167.                                 Thread3.flag=true;
  168.                         }
  169.                 });
  170.                 buttonstop3.addActionListener(new ActionListener() {

  171.                         @Override
  172.                         public void actionPerformed(ActionEvent arg0) {
  173.                                 Thread3.flag=false;
  174.                         }
  175.                 });
  176.         }
  177. }
  178. class Thread1 extends Thread
  179. {
  180.         public static boolean flag=true;
  181.         JProgressBar progressBar;
  182.         private static int i=0;
  183.         public Thread1(JProgressBar progressBar)
  184.         {
  185.                 this.progressBar=progressBar;
  186.         }
  187.         @Override
  188.         public void run() {

  189.                         if (i<=100) {
  190.                                 i+=1;
  191.                                 progressBar.setValue(i);
  192.                                                 try {
  193.                                                 Thread.sleep(30);
  194.                                         } catch (InterruptedException e) {

  195.                                                 e.printStackTrace();
  196.                                         }
  197.                         }
  198.                         else {
  199.                                 i=0;
  200.                         }
  201.         }
  202. }
  203. class Thread2 extends Thread
  204. {
  205.         public static boolean flag=true;
  206.         JProgressBar progressBar;
  207.         private static int i=0;
  208.         public Thread2(JProgressBar progressBar)
  209.         {
  210.                 this.progressBar=progressBar;
  211.         }
  212.         @Override
  213.         public void run() {
  214.                         if (i<=100) {
  215.                                 i+=1;
  216.                                 progressBar.setValue(i);

  217.                                 try {
  218.                                         Thread.currentThread().sleep(30);
  219.                                 } catch (InterruptedException e) {
  220.                                         e.printStackTrace();
  221.                                 }
  222.                         }
  223.                         else {
  224.                                 i=0;
  225.                         }
  226.         }
  227. }
  228. class Thread3 extends Thread
  229. {
  230.         public static boolean flag=true;
  231.         JProgressBar progressBar;
  232.         private static int i=0;
  233.         public Thread3(JProgressBar progressBar)
  234.         {
  235.                 this.progressBar=progressBar;
  236.         }
  237.         @Override
  238.         public void run() {
  239.                         if (i<=100) {
  240.                                 i+=1;
  241.                                 progressBar.setValue(i);
  242.                                 try {
  243.                                         Thread.currentThread().sleep(30);
  244.                                 } catch (InterruptedException e) {
  245.                                         e.printStackTrace();
  246.                                 }
  247.                         }
  248.                         else {
  249.                                 i=0;
  250.                         }
  251.         }
  252. }
复制代码


评分

参与人数 1黑马币 +6 收起 理由
w239983684 + 6 很给力!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马