2012-03-14 104 views
1

我在并行运行两个线程时遇到问题。每个线程单独运行良好。我的要求是显示10个球,一个红球,如果value0和一个绿球,如果value1,一个接一个。 value中的数据是从包含0 s或1 s的阵列中收到的。我需要一起运行16个这样的线程。我目前正在尝试两个。多线程和Swing

package pkg2; 
public class mainClass { 

public static void main(String[] args) { 
    Intermediate frame = new Intermediate(); 
    } 
} 

主要类调用中间类

package pkg2; 

import java.awt.Color; 
import javax.swing.*; 

public class Intermediate extends JFrame { 

    public Intermediate() { 
     DivScreen ob = new DivScreen(); 
     ob.setBackground(Color.black); 
     ob.divScreen1(16); 
     add(ob); 
     pack(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     setSize(1370, 740); 
     setResizable(false); 
    } 
} 

在一个中间类,DivScreen类的一个对象时,其中,所有的线程和GUI部分就完成了。

package pkg2; 

import java.awt.*; 
import javax.swing.*; 

public class DivScreen extends Canvas implements Runnable//,ActionListener 
{ 

    Thread t1[];   //threads 
    int i, j;    //n=total no. of lines, i=no. of rows, j=no of columns 
    public static int x; // x is now global variable 
    public static int i1 = 0, i2 = 0; //to continue fetching data from last entry 
    public static int c1 = 0, c2 = 0; // to check whether line is working or not 
    public static int y1, y2; // to show red or green balls 
    public static int k1 = 0, k2 = 0; //to draw 10 balls 
    int green, blue, red; //variables for color of lines 
    int arr1[] = {1, 1, 0, 1}; 
    int arr2[] = {1, 0, 1, 0, 1, 0}; 

    public DivScreen() //default. constructor 
    { 
     //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Font f = new Font("Arial", Font.BOLD, 30); 
     setFont(f); 
    } 

    public void divScreen1(int m) { 
     t1 = new Thread[2]; //HERE WE HAVE TO PAAS n AS SIZE OF THREAD ARRAY 
     // BUT JUST TO CHECK ITS WORKING WE ARE USING 2 THREADS 
     for (int i = 0; i < 2; i++) { 
      t1[i] = new Thread(this, (i + 1) + "thread"); 
      t1[i].start(); 
     } 
    } 

    public void paint(Graphics g) { 
     g.setColor(Color.white); 

     for (int i = 1; i < 4; i++) { 
      j = i;        //j is for horizontal lines 
      g.drawLine(i * 342, 0, i * 342, 740); //i is for vertical lines 
      g.drawLine(0, j * 185, 1370, j * 185); 
     } 
     if (x == 1) { 
      g.setColor(Color.GRAY); 
      g.drawString("Line 1", 150, 50); 
      if (c1 == 0) { 
       g.drawString("Line is not in use", 30, 150); 
       g.setColor(Color.black); 
       g.fillRect(45, 90, 200, 30); 
      } else { 
       g.setColor(Color.black); 
       g.fillRect(30, 120, 250, 30); 
       if (k1 < 10) { 
        if (y1 == 0) { 
         g.setColor(Color.red); 
        } else { 
         g.setColor(Color.green); 
        } 
        g.fillOval(50 + 20 * (k1++), 100, 15, 15); 
       } else { 
        k1 = 0; 
        g.setColor(Color.black); 
        g.fillRect(45, 90, 200, 30); 
       } 
      } 
     } 

     if (x == 2) { 
      g.setColor(Color.gray); 
      g.drawString("Line 2", 460, 50); 
      if (c2 == 0) { 
       g.drawString("Line is not in use", 370, 150); 
       g.setColor(Color.black); 
       g.fillRect(385, 90, 200, 30); 
      } else { 
       g.setColor(Color.black); 
       g.fillRect(370, 120, 250, 30); 
       if (k2 < 10) { 
        if (y2 == 0) { 
         g.setColor(Color.red); 
        } else { 
         g.setColor(Color.green); 
        } 
        g.fillOval(390 + 20 * (k2++), 100, 15, 15); 
       } else { 
        k2 = 0; 
        g.setColor(Color.black); 
        g.fillRect(385, 90, 200, 30); 
       } 
      } 
     } 
    } 

    public void update(Graphics g) { 
     paint(g); 
    } 

    public void run() { 
     while (true) { 
      if (Thread.currentThread().getName().equals("1thread")) { 
       x = 1; 
       int value = 0;    // to get value from array    
       while (i1 < 4) { 
        c1 = 1; 
        value = arr1[i1]; //valid is a value containing 1 or 0 
        i1++;    // 1 implies product is OK, 0 implies product not OK 

        System.out.println(value); 
        if (value == 1) { 
         y1 = 1;   // we will check its value in paint() function 
        } else { 
         y1 = 0; 
        } 
        SwingUtilities.invokeLater(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          repaint(0, 0, 342, 185); 
         } 
        }); 


        try { 
         Thread.sleep(200); 
        } catch (Exception e) { 
         System.out.println(e); 
        } 
       } 

       c1 = 0; 
       SwingUtilities.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         repaint(0, 0, 342, 185); 
        } 
       }); 


       try { 
        Thread.sleep(200); 
       } catch (Exception e) { 
        System.out.println(e); 
       } 
      } 

      if (Thread.currentThread().getName().equals("2thread")) { 
       x = 2; 
       int value2 = 0;     // to get value from arr2[] 

       while (i2 < 6) { 
        c2 = 1; 
        value2 = arr2[i2]; 
        i2++; 
        System.out.println(value2); 
        if (value2 == 1) { 
         y2 = 1; 
        } else { 
         y2 = 0; 
        } 
        SwingUtilities.invokeLater(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          repaint(342, 0, 342, 185); 
         } 
        }); 

        try { 
         Thread.sleep(200); 
        } catch (Exception e) { 
         System.out.println(e); 
        } 

       } 

       c2 = 0; 
       SwingUtilities.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         repaint(342, 0, 342, 185); 
        } 
       }); 

       try { 
        Thread.sleep(200); 
       } catch (Exception e) { 
        System.out.println(e); 
       } 
      } 
     } 
    } 
} 

请回复,如果你能找到一些东西。 谢谢。

+0

拥有一个既是JComponent也是Runnable的类看起来很奇怪。 – toto2 2012-03-14 11:47:10

+1

请勿在没有充分理由的情况下混用Swing(例如'JFrame')和AWT(例如'Canvas')组件。为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2012-03-14 12:17:43

回答

1

请注意多线程。

只是一个例子:

  • x是要由两个线程被改变并且在涂料还用于()的一个静态变量。在绘制()开始或执行期间,您不能保证x的状态。线程可能会随意更改它。

我建议你多读一点线程和并发性,以及如何管理它,但我会说最大的问题是程序本身的设计。你到底在做什么,为什么需要多线程?如果您想对绘画的内容进行大量计算,可以使用多个线程进行这些计算,但绘画方法应以同时安全的方式接收这些信息。应该只有一个单独的线程执行绘画作业,并且在绘画过程中使用的信息应该被锁定。