2013-11-24 3 views
0

lable 값을 0에서 n까지 증가시키는 스레드 클래스가 있습니다. 스레드 코드 : stop 값이 true이 아닌 때까지스레드주기에서 단계를 만드는 방법은 무엇입니까?

public class 

    SimpleThread implements Runnable { 

      Thread t; 
      volatile boolean stop = true; 
     int n; 
     JLabel label; 
     String name; 

     SimpleThread(String name) { 
      this.name = name; 
      t = new Thread(this, "settingThread"); 
      t.start(); 
     } 


     public void run() { 
      while (true) { 

       while (stop) { 


       } 

       System.out.println(name + " stop:" + stop); 
       for (int i = 0; i < n; i++) { 
        label.setText(String.valueOf(i)); 
        try { 
         Thread.sleep(100); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
        } 
       } 
       stop = true; 

      } 
     } 

     public void start(int n, JLabel label) { 
      stop = false; 
      this.n = n; 
      this.label = label; 
     } 
    } 

스레드 작업해야합니다. 또한 나는 다른 인수와 함께 2 스레드 객체 st1st2있어 :

st1.start(10, timeLabel1); 
st2.start(5, timeLabel2); 

그래서 첫 번째 스레드가 0 ~ 10과 st2에서 timeLabel1를 증가한다 - 0에서 지금 5. 그리고 질문에. 싸이클에서 쓰레드가 무대를 통과 할 때까지 어떻게 프로그램을 대기시킬 수 있습니까? 다음

public void run() { 
      while (true) { 

       while (stop) { 


       } 

       System.out.println(name + " stop:" + stop); 
       for (int i = 0; i < n; i++) { 
        label.setText(String.valueOf(i)); 
        try { 
         Thread.sleep(100); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
        } 
       } 
       stop = true; 
       //stage 

      } 
     } 

그리고 : :이 같은 의미

st1.start(10, timeLabel1); 
     st2.start(5, timeLabel2); 
     //wait until both stages aren't done 
     System.out.println("yep"); 

코드 :

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* Created with IntelliJ IDEA. 
* User: Администратор 
* Date: 22.11.13 
* Time: 21:17 
* To change this template use File | Settings | File Templates. 
*/ 
public class StackOverflow implements ActionListener { 
    JButton button = new JButton("Button"); 

    SimpleThread st1 = new SimpleThread("1st"), st2 = new SimpleThread("2nd"); 

    JLabel timeLabel1 = new JLabel("0"), timeLabel2 = new JLabel("0"); 


    StackOverflow() { 


     JFrame jfrm = new JFrame("Strategy fighting"); 
     jfrm.setLayout(new FlowLayout()); 
     jfrm.setSize(300, 150); 

     jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     button.addActionListener(this); 
     jfrm.add(button); 
     jfrm.add(timeLabel1); 
     jfrm.add(timeLabel2); 


     jfrm.setVisible(true); 
    } 


    public class SimpleThread implements Runnable { 

     Thread t; 
     volatile boolean stop = true; 
     int n; 
     JLabel label; 
     String name; 

     SimpleThread(String name) { 
      this.name = name; 
      t = new Thread(this, "settingThread"); 
      t.start(); 
     } 


     public void run() { 
      while (true) { 

       while (stop) { 


       } 

       System.out.println(name + " stop:" + stop); 
       for (int i = 0; i < n; i++) { 
        label.setText(String.valueOf(i)); 
        try { 
         Thread.sleep(100); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
        } 
       } 
       stop = true; 


      } 
     } 

     public void start(int n, JLabel label) { 
      stop = false; 
      this.n = n; 
      this.label = label; 
     } 
    } 


    public void actionPerformed(ActionEvent e) { 

     st1.start(10, timeLabel1); 
     st2.start(5, timeLabel2); 

     System.out.println("yep"); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       new StackOverflow(); 
      } 
     }); 
    } 
} 
+1

대기를하고 할 수 있어야 통지 이 작업을 수행. http://www.programcreek.com/2009/02/notify-and-wait-example/ –

+0

코드가 너무 많고 원하는 내용이 너무 적습니다. Лениво разбираться. –

답변

0

귀하의 질문에 이미 답이 다음 Phaser 클래스를. 당신의 스레드에서

static final Phaser phaser = new Phaser(NUM_THREADS + 1); // +1 for the main thread 

:

run() { 
    // do stage 1 
    phaser.arriveAndAwaitAdvance(); // wait until everyone is done with stage 1 
    // do stage 2 
} 

그리고 주에서 :)이 같은

뭔가 트릭을 할해야

startThreads(NUM_THREADS); 
phaser.arriveAndAwaitAdvance(); // wait until stage 1 is done 
// ... do something else 
+0

하지만 내 스레드가 주기적으로 작동합니다. 그래서 1 차 반복 후에 나의 스레드는 모든 시간 동안 1 단계가 될 것입니다. – Tony

+0

귀하의 문제를 이해할 수 있을지 잘 모르겠습니다. 페이저를 다시 사용하지 않으려는 경우에는 필요하지 않습니다. Phaser에서 고객을 등록 취소 할 수도 있습니다. 주 스레드를 첫 번째 사이클에서만 동기화하려는 경우 주 스레드의 등록을 취소 할 수 있습니다. – TwoThe

관련 문제