• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Different ways of implementing Thread , whats the difference based on output

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ouyput

Thread A: 0
Thread B: 1
Thread C: 0
Thread D: 0
Thread A: 2
Thread B: 3
Thread C: 1
Thread D: 1
Thread A: 4
Thread B: 5
Thread C: 2
Thread D: 2
Thread A: 6
Thread B: 7
Thread C: 3
Thread D: 3
Thread A: 8
Thread B: 9
Thread C: 4
Thread D: 4

In the code above it seems that when we implements Runnable interface then , all threads share the same object and if we extends Thread class then each thread have its different copy of variables.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right.
The only difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas with Runnable, many threads share the same object instance.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by wise owen:
You are right.
The only difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas with Runnable, many threads share the same object instance.



Isn't that only because he passed the 'same' Runner object that implements Runnable to each new Thread that he created?

I just wanted to clarify so the OP wasn't confused thinking that delcaring Threads using the Runnable interface always means they are going to share the same object. Maybe it should be phrased that "many threads 'can' share the same object".

If he did...

Thread t1 = new Thread( new Runner(5), "Thread A");
Thread t2 = new Thread( new Runner(5), "Thread B");

each Thread would be working with their own instance of Runner (at least that's my understanding).
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's change it to

The one difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas with Runnable, many threads CAN share the same object instance.
[ July 07, 2006: Message edited by: wise owen ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try the following program ...

public class ThreadTestFinal{
public static void main (String[] args){
Runner r = new Runner(5);
Strider s = new Strider(5);
Thread t1 = new Thread(r, "Thread A");
Thread t2 = new Thread(r, "Thread B");
Thread s1 = new Thread(s, "Thread C");
Thread s2 = new Thread(s, "Thread D");
t1.start();
t2.start();
s1.start();
s2.start();
}
}

class Runner implements Runnable {
private int counter;
private final int MAX_LIMIT;
Runner(int limit) {
MAX_LIMIT = limit;
}

public void run() {
try {
for (int i=0; i<MAX_LIMIT; i++) {
System.out.println(Thread.currentThread().getName() + ": " + counter++);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
e.printStackTrace();
}

}
}


class Strider extends Thread {
private int counter;
private final int MAX_LIMIT;

Strider(int limit) {
MAX_LIMIT = limit;
}
Strider(int limit, String name) {
MAX_LIMIT = limit;
setName(name);
}

public void run() {
try {
for (int i=0; i<MAX_LIMIT; i++) {
System.out.println(Thread.currentThread().getName() + ": " + counter++);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}


the thing is Thread class intern implements Runnable and thus it can also be treated like a Runnable (many threads CAN share the same object instance.. as you said. ).

pls correct me if i am wrong.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strider s = new Strider(5);
Thread s1 = new Thread(s, "Thread C");
Thread s2 = new Thread(s, "Thread D");

If you do this way, and what is your purpose to extends your class from Thread? Why not just implement it from Runnable.
 
HungryJavaGoat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah .. you are right. i just did for an example ... to show that it depends on the runnable object(whether it implements Runnable or extends thread in which later is case not needed as you said) you are sending as input for thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic