2012-03-14 77 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的狀態。線程可能會隨意更改它。

我建議你多讀一點線程和併發性,以及如何管理它,但我會說最大的問題是程序本身的設計。你到底在做什麼,爲什麼需要多線程?如果您想對繪畫的內容進行大量計算,可以使用多個線程進行這些計算,但繪畫方法應以同時安全的方式接收這些信息。應該只有一個單獨的線程執行繪畫作業,並且在繪畫過程中使用的信息應該被鎖定。