This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
java:concurrency_intro [2015/03/24 13:50] gthanos [Περιμένοντας ένα νήμα να ολοκληρώσει μέσω της μεθόδου join()] |
java:concurrency_intro [2015/03/24 14:01] gthanos [Περιμένοντας ένα νήμα να ολοκληρώσει μέσω της μεθόδου join()] |
||
---|---|---|---|
Line 117: | Line 117: | ||
* **t.join(long millis, int nanos):** μέχρι το νήμα t να τερματίσει ή περίμενε για **millis+nanos/1000000** milliseconds. | * **t.join(long millis, int nanos):** μέχρι το νήμα t να τερματίσει ή περίμενε για **millis+nanos/1000000** milliseconds. | ||
- | Όπως και η μέθοδος sleep() η μέθοδος join() δύναται να διακοπεί από ένα InterruptedException(). | + | Όπως και η μέθοδος sleep() η μέθοδος join() δύναται να διακοπεί από ένα [[http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html|InterruptedException()]]. |
===== Εφαρμόζοντας τις μεθόδους sleep()/interrupt()/join() σε ένα παράδειγμα ===== | ===== Εφαρμόζοντας τις μεθόδους sleep()/interrupt()/join() σε ένα παράδειγμα ===== | ||
Δείτε το παρακάτω παράδειγμα εκτέλεσης δύο νημάτων, το οποίο χρησιμοποιεί τις μεθόδους sleep(), interrupt() και join() | Δείτε το παρακάτω παράδειγμα εκτέλεσης δύο νημάτων, το οποίο χρησιμοποιεί τις μεθόδους sleep(), interrupt() και join() | ||
+ | |||
+ | <code java MessageLoops.java> | ||
+ | public class MessageLoop implements Runnable { | ||
+ | public void run() { | ||
+ | String importantInfo[] = { | ||
+ | "Mares eat oats", | ||
+ | "Does eat oats", | ||
+ | "Little lambs eat ivy", | ||
+ | "A kid will eat ivy too" | ||
+ | }; | ||
+ | try { | ||
+ | for (int i = 0; | ||
+ | i < importantInfo.length; | ||
+ | i++) { | ||
+ | // Pause for 4 seconds | ||
+ | Thread.sleep(4000); | ||
+ | // Print a message | ||
+ | SimpleThreads.threadMessage(importantInfo[i]); | ||
+ | } | ||
+ | } catch (InterruptedException e) { | ||
+ | SimpleThreads.threadMessage("I wasn't done!"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
<code java SimpleThreads.java> | <code java SimpleThreads.java> | ||
public class SimpleThreads { | public class SimpleThreads { | ||
- | // Display a message, preceded by | + | // Display a message, preceded by |
- | // the name of the current thread | + | // the name of the current thread |
- | static void threadMessage(String message) { | + | public static void threadMessage(String message) { |
- | String threadName = | + | String threadName = |
- | Thread.currentThread().getName(); | + | Thread.currentThread().getName(); |
- | System.out.format("%s: %s%n", | + | System.out.format("%s: %s%n", |
- | threadName, | + | threadName, |
- | message); | + | message); |
- | } | + | } |
- | private static class MessageLoop | + | public static void main(String args[]) |
- | implements Runnable { | + | throws InterruptedException { |
- | public void run() { | + | |
- | String importantInfo[] = { | + | |
- | "Mares eat oats", | + | |
- | "Does eat oats", | + | |
- | "Little lambs eat ivy", | + | |
- | "A kid will eat ivy too" | + | |
- | }; | + | |
- | try { | + | |
- | for (int i = 0; | + | |
- | i < importantInfo.length; | + | |
- | i++) { | + | |
- | // Pause for 4 seconds | + | |
- | Thread.sleep(4000); | + | |
- | // Print a message | + | |
- | threadMessage(importantInfo[i]); | + | |
- | } | + | |
- | } catch (InterruptedException e) { | + | |
- | threadMessage("I wasn't done!"); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | public static void main(String args[]) | + | // Delay, in milliseconds before |
- | throws InterruptedException { | + | // we interrupt MessageLoop |
+ | // thread (default one hour). | ||
+ | long patience = 1000 * 60 * 60; | ||
- | // Delay, in milliseconds before | + | // If command line argument |
- | // we interrupt MessageLoop | + | // present, gives patience |
- | // thread (default one hour). | + | // in seconds. |
- | long patience = 1000 * 60 * 60; | + | if (args.length > 0) { |
+ | try { | ||
+ | patience = Long.parseLong(args[0]) * 1000; | ||
+ | } catch (NumberFormatException e) { | ||
+ | System.err.println("Argument must be an integer."); | ||
+ | System.exit(1); | ||
+ | } | ||
+ | } | ||
- | // If command line argument | + | threadMessage("Starting MessageLoop thread"); |
- | // present, gives patience | + | long startTime = System.currentTimeMillis(); |
- | // in seconds. | + | Thread t = new Thread(new MessageLoop()); |
- | if (args.length > 0) { | + | t.start(); |
- | try { | + | |
- | patience = Long.parseLong(args[0]) * 1000; | + | |
- | } catch (NumberFormatException e) { | + | |
- | System.err.println("Argument must be an integer."); | + | |
- | System.exit(1); | + | |
- | } | + | |
- | } | + | |
- | threadMessage("Starting MessageLoop thread"); | + | threadMessage("Waiting for MessageLoop thread to finish"); |
- | long startTime = System.currentTimeMillis(); | + | // loop until MessageLoop |
- | Thread t = new Thread(new MessageLoop()); | + | // thread exits |
- | t.start(); | + | while (t.isAlive()) { |
- | + | threadMessage("Still waiting..."); | |
- | threadMessage("Waiting for MessageLoop thread to finish"); | + | // Wait maximum of 1 second |
- | // loop until MessageLoop | + | // for MessageLoop thread |
- | // thread exits | + | // to finish. |
- | while (t.isAlive()) { | + | t.join(1000); |
- | threadMessage("Still waiting..."); | + | if (((System.currentTimeMillis() - startTime) > patience) |
- | // Wait maximum of 1 second | + | && t.isAlive()) { |
- | // for MessageLoop thread | + | threadMessage("Tired of waiting!"); |
- | // to finish. | + | t.interrupt(); |
- | t.join(1000); | + | // Shouldn't be long now |
- | if (((System.currentTimeMillis() - startTime) > patience) | + | // -- wait indefinitely |
- | && t.isAlive()) { | + | t.join(); |
- | threadMessage("Tired of waiting!"); | + | } |
- | t.interrupt(); | + | |
- | // Shouldn't be long now | + | |
- | // -- wait indefinitely | + | |
- | t.join(); | + | |
- | } | + | |
- | } | + | |
- | threadMessage("Finally!"); | + | |
} | } | ||
- | } | + | threadMessage("Finally!"); |
- | </code> | + | } |
+ | }</code> | ||