Creating a thread by extending the Thread class
package [Link].thread1;
public class CountDownApp {
public static void main(String[] args) {
// Instantiate the countdown threads
Thread count1 = new CountDownEven();
Thread count2 = new CountDownOdd();
// Start the countdown threads
[Link]();
[Link]();
}
}
package [Link].thread1;
// This class counts even numbers
public class CountDownEven extends Thread {
public void run() {
for (int i = 10; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
[Link](); // Allow the other thread to run
}
}
}
package [Link].thread1;
// This class counts odd numbers
public class CountDownOdd extends Thread {
public void run() {
for (int i = 9; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
[Link](); // Allow the other thread to run
}
}
}
Resulting output
Thread-0 Count 10
Thread-1 Count 9
Thread-0 Count 8
Thread-1 Count 7
Thread-0 Count 6
Thread-1 Count 5
Thread-0 Count 4
Thread-1 Count 3
Thread-0 Count 2
Thread-1 Count 1
Creating a thread by implementing the Runnable interface
package [Link].thread2;
public class CountDownRunnableApp {
public static void main(String[] args) {
// Instantiate the countdown threads
Thread count1 = new Thread(new CountDownEven());
Thread count2 = new Thread(new CountDownOdd());
// Start the countdown threads
[Link]();
[Link]();
}
}
package [Link].thread2;
public class CountDownEven implements Runnable {
public void run() {
Thread t = [Link]();
for (int i = 10; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
[Link](); // Allow the other thread to run
}
}
}
package [Link].thread2;
public class CountDownOdd implements Runnable {
public void run() {
Thread t = [Link]();
for (int i = 9; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
[Link](); // Allow the other thread to run
}
}
}
Resulting output
Thread-0 Count 10
Thread-1 Count 9
Thread-0 Count 8
Thread-1 Count 7
Thread-0 Count 6
Thread-1 Count 5
Thread-0 Count 4
Thread-1 Count 3
Thread-0 Count 2
Thread-1 Count 1
A version of the Count Down application that uses sleep() rather than yield()
package [Link].thread3;
public class CountDownSleepApp {
public static void main(String[] args) {
// Instantiate the countdown threads
Thread count1 = new Thread(new CountDownEven());
Thread count2 = new Thread(new CountDownOdd());
// Start the countdown threads
[Link]();
[Link]();
}
}
package [Link].thread3;
public class CountDownEven implements Runnable {
public void run() {
Thread t = [Link]();
for (int i = 10; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
try {
[Link](500); // Sleep for 2 seconds
} catch (InterruptedException e) {} // Ignore any interruptions
}
}
}
package [Link].thread3;
public class CountDownOdd implements Runnable {
public void run() {
Thread t = [Link]();
for (int i = 9; i > 0; i -= 2) {
[Link]([Link]() + " Count " + i);
try {
[Link](2000); // Sleep for 2 seconds
} catch (InterruptedException e) {} // Ignore any interruptions
}
}
}
Resulting output
Thread-0 Count 10
Thread-1 Count 9
Thread-0 Count 8
Thread-0 Count 6
Thread-0 Count 4
Thread-0 Count 2
Thread-1 Count 7
Thread-1 Count 5
Thread-1 Count 3
Thread-1 Count 1
A version of the Count Down application that sets thread priorities
package [Link].thread2;
public class CountDownPrioritiesApp {
public static void main(String[] args) {
// Instantiate the countdown threads
Thread count1 = new Thread(new CountDownEven());
Thread count2 = new Thread(new CountDownOdd());
// Set the thread priorities
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
// Start the countdown threads
[Link]();
[Link]();
}
}
Resulting output
Thread-1 Count 9
Thread-1 Count 7
Thread-1 Count 5
Thread-1 Count 3
Thread-1 Count 1
Thread-0 Count 10
Thread-0 Count 8
Thread-0 Count 6
Thread-0 Count 4
Thread-0 Count 2