Sie sind auf Seite 1von 6

1 Source Code

1.1 simpleMessage Class (original code)

public static void main(String args[]) throws


InterruptedException {

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);
}

threadMessage("Starting MessageLoop thread");


long startTime = System.currentTimeMillis();
Thread t = new Thread(new MessageLoop());
t.start();

threadMessage("Waiting for MessageLoop thread to finish");


while (t.isAlive()) {
threadMessage("Still waiting...");
t.join(1000);
if (((System.currentTimeMillis() - startTime) >
patience) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
//Shouldn't be long now -- wait indefinitely
t.join();
}
}

threadMessage("Finally!");

}
Code 1: main() Method

static void threadMessage(String message) {


String threadName = Thread.currentThread().getName();
System.out.format("%s: %s%n", threadName, message);
}
Code 2: threadMessage() Method

1.1.1 MessageLoop Class (inside simpleMessage Class)

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++) {
Thread.sleep(4000);
threadMessage(importantInfo[i]);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
Code 3: run() Method
2.1 modifiedMessage Class (modified code)

public static void main(String args[]) throws InterruptedException


{
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);
}

threadMessage("Starting MessageLoop thread");


long startTime = System.currentTimeMillis();
Thread t = new Thread(new MessageLoop());
t.start();
threadMessage("Waiting for MessageLoop thread to finish");

while (t.isAlive()) {
threadMessage("Still waiting...");
t.join(1000);
if (((System.currentTimeMillis() - startTime) >
patience) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
t.join();
}
}

threadMessage("Finally!");

}
Code 4: main() Method

static void threadMessage(String message) {


String threadName = Thread.currentThread().getName();
System.out.format("%s: %s%n", threadName, message);
}
Code 5: threadMessage() Method
2.1.1 MessageLoop Class (inside modifiedMessage Class)

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++) {
Thread.sleep(4000);
threadMessage(importantInfo[i]);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
Code 6: run() Method

2 Output (Debugging)

2.1 simpleMessage Class

run:
main: Starting MessageLoop thread
main: Waiting for MessageLoop thread to finish
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Mares eat oats
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Does eat oats
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Little lambs eat ivy
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: A kid will eat ivy too
main: Still waiting...
main: Finally!
Output 1: simpleMessage Class Output Result

2.2 modifiedMessage Class

run:
main: Starting MessageLoop thread
main: Waiting for MessageLoop thread to finish
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Good Morning
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Good Afternoon
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Good Evening
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: Good Night
main: Finally!
Output 2: modifiedMessage Class Output Result

3 Conclusion

The use of threads in Java and distributed system will make things more efficient and fast. In
the above example, its uses “Thread-0” to display messages using MessageLoop class. The
main class is the one that execute threads to assign some to other classes such as
MessageLoop in above example.

While the MessageLoop class is running, the main class is checking for the thread every
second else it will go to idle mode and end the task.

Das könnte Ihnen auch gefallen