0% found this document useful (0 votes)
28 views6 pages

Extending Thread Class in Java

The document describes different ways to create and run threads in Java: 1) By extending the Thread class and overriding the run() method. 2) By implementing the Runnable interface and overriding the run() method. 3) Using the sleep() method instead of yield() to pause threads for a period of time. 4) Setting different priorities for threads to influence their scheduling order.

Uploaded by

Swapna Pilly
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Extending Thread Class in Java

The document describes different ways to create and run threads in Java: 1) By extending the Thread class and overriding the run() method. 2) By implementing the Runnable interface and overriding the run() method. 3) Using the sleep() method instead of yield() to pause threads for a period of time. 4) Setting different priorities for threads to influence their scheduling order.

Uploaded by

Swapna Pilly
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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

You might also like