Sei sulla pagina 1di 6

Creating a thread by extending the Thread class

package edu.niu.cs.mcmahon.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


count1.start();
count2.start();
}
}

package edu.niu.cs.mcmahon.thread1;

// This class counts even numbers


public class CountDownEven extends Thread {

public void run() {


for (int i = 10; i > 0; i -= 2) {
System.out.println(this.getName() + " Count " + i);
Thread.yield(); // Allow the other thread to run
}
}
}

package edu.niu.cs.mcmahon.thread1;

// This class counts odd numbers


public class CountDownOdd extends Thread {

public void run() {


for (int i = 9; i > 0; i -= 2) {
System.out.println(this.getName() + " Count " + i);
Thread.yield(); // 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 edu.niu.cs.mcmahon.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


count1.start();
count2.start();
}
}

package edu.niu.cs.mcmahon.thread2;

public class CountDownEven implements Runnable {

public void run() {


Thread t = Thread.currentThread();
for (int i = 10; i > 0; i -= 2) {
System.out.println(t.getName() + " Count " + i);
Thread.yield(); // Allow the other thread to run
}
}
}

package edu.niu.cs.mcmahon.thread2;

public class CountDownOdd implements Runnable {

public void run() {


Thread t = Thread.currentThread();
for (int i = 9; i > 0; i -= 2) {
System.out.println(t.getName() + " Count " + i);
Thread.yield(); // 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 edu.niu.cs.mcmahon.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


count1.start();
count2.start();
}
}

package edu.niu.cs.mcmahon.thread3;

public class CountDownEven implements Runnable {

public void run() {


Thread t = Thread.currentThread();
for (int i = 10; i > 0; i -= 2) {
System.out.println(t.getName() + " Count " + i);
try {
Thread.sleep(500); // Sleep for 2 seconds
} catch (InterruptedException e) {} // Ignore any interruptions
}
}
}

package edu.niu.cs.mcmahon.thread3;

public class CountDownOdd implements Runnable {

public void run() {


Thread t = Thread.currentThread();
for (int i = 9; i > 0; i -= 2) {
System.out.println(t.getName() + " Count " + i);
try {
Thread.sleep(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 edu.niu.cs.mcmahon.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


count1.setPriority(Thread.MIN_PRIORITY);
count2.setPriority(Thread.MAX_PRIORITY);

// Start the countdown threads


count1.start();
count2.start();
}
}

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

Potrebbero piacerti anche