2015-12-18 10 views
8

मैं एक जावा प्रोग्राम लिख रहा हूं जो प्रिंट सेकंड को समाप्त कर देता है, और हर 5 वें सेकेंड में यह एक संदेश प्रिंट करेगा। यह एक नमूना आउटपुट है:अधिसूचना के साथ एकाधिक धागे सभी()

0 1 2 3 4 hello 5 6 7 8 9 hello 10 11 12 13 14 hello 15 16 17 18 19 hello 

मैं बुलियन परिवर्तनीय printMsg को कैसे हटा सकता हूं? क्या कोई बेहतर थ्रेड डिज़ाइन है जो इसकी अनुमति देता है?

अभी के लिए, printMsg बिना कार्यक्रम 1/10 दूसरा 5 में रह कार्यक्रम के दौरान कई "हैलो" प्रिंट होगा, 10, 15 आदि

class Timer { 
    private int count = 0; 
    private int N; 
    private String msg; 
    private boolean printMsg = false; 

    public Timer(String s, int N) { 
     msg = s; 
     this.N = N; 
    } 

    public synchronized void printMsg() throws InterruptedException{ 
     while (count % N != 0 || !printMsg) 
      wait(); 
     System.out.print(msg + " "); 
     printMsg = false; 
    } 

    public synchronized void printTime() { 
     printMsg = true; 
     System.out.print(count + " "); 
     count ++; 
     notifyAll(); 
    } 

    public static void main(String[] args) { 
     Timer t = new Timer("hello", 5); 
     new TimerThread(t).start(); 
     new MsgThread(t).start(); 
    } 
} 

class TimerThread extends Thread { 
    private Timer t; 
    public TimerThread(Timer s) {t = s;} 

    public void run() { 
     try { 
      for(;;) { 
       t.printTime(); 
       sleep(100); 
      } 
     } catch (InterruptedException e) { 
      return; 
     } 
    } 
} 

class MsgThread extends Thread { 
    private Timer t; 
    public MsgThread(Timer s) {t = s;} 

    public void run() { 
     try { 
      for(;;) { 
       t.printMsg(); 
      } 
     } catch (InterruptedException e) { 
      return; 
     } 
    } 
} 
+0

रखो 'गिनती% एन की आवश्यकता नहीं है =' printTime' में 0' जांच और केवल कॉल 'notifyAll' जब यह 'सत्य' – MadProgrammer

+0

है तो यह 100 एमएस के बजाय 1000 एमएस सोना नहीं चाहिए। क्योंकि आप हर 5 सेकंड की नींद (1000) 'संदेश प्रिंट करना चाहते हैं; 'नींद (100)' के बजाय; इस कोड के साथ यह हर 500 एमएस प्रिंटिंग संदेश है .. –

उत्तर

0

printMsg झंडा उपयोग करने के लिए, बस notifyAll जब count % N == 0

public synchronized void printMsg() throws InterruptedException { 
    wait(); 
    System.out.print(msg + " ");   
} 

public synchronized void printTime() {  
    System.out.print(count + " "); 
    count++; 
    if (count % N == 0){ 
     notifyAll();  
    }    
} 
0

एक विकल्प आसान बनाने के लिए और बेहतर डिजाइन का उपयोग करने के लिए है दो धागे के बजाय एकल धागा। और एकल धागा प्रिंटिंग सेकंड के साथ ही संदेश की देखभाल करते हैं। इस तरह एक धागा को कम करें और प्रतीक्षा() और सूचित करने की आवश्यकता नहीं है। क्या कोई कारण है कि आप दो धागे का उपयोग क्यों करना चाहते हैं? नीचे दिए गए कोड:!

public class Timer { 
    private int count = 0; 
    private int N; 
    private String msg; 

    public Timer(String s, int N) { 
     msg = s; 
     this.N = N; 
    } 

    public synchronized void printTime() { 
     System.out.print(count + " "); 
     count ++; 
     if(count % N == 0) { 
      System.out.print(msg + " "); 
     } 
    } 

    public static void main(String[] args) { 
     Timer t = new Timer("hello", 5); 
     new TimerThread(t).start(); 
    } 
} 

class TimerThread extends Thread { 
    private Timer t; 
    public TimerThread(Timer s) {t = s;} 

    public void run() { 
     try { 
      for(;;) { 
       t.printTime(); 
       sleep(1000); 
      } 
     } catch (InterruptedException e) { 
      return; 
     } 
    } 
} 
संबंधित मुद्दे