Reputation: 674
I am trying to work out how to run NamedRunnable from my public static void main. I am essentially experimenting with 2 ways to run my thread the first is to create and define a thread the next is to define a class and then implement runnable.
Here is my code
package threads;
public class Threads extends Thread{
private final String name; // The name of this thread
public static void main(String[] args) {
long lStartTime = System.nanoTime();
Threads greetings = new Threads("Fred");
Threads greetings1 = new Threads("Betty");
NamedRunnable greetings2 = new NamedRunnable("Ralph");//it is here i cant seem to create an instance of Named Runnable and therefore call start
greetings.start();
greetings1.start();
greetings2.start();
long lEndTime = System.nanoTime();
long difference = lEndTime - lStartTime;
System.out.println("Elapsed time: " + difference);
}
public Threads(String name) {
this.name = name;
}
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from thread ’" + name + "’!");
}
public class NamedRunnable implements Runnable {
private final String name;
// The name of this Runnable.
public NamedRunnable(String name) { // Constructor gives name to object.
this.name = name; }
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from runnable ’" + name +"’!"); } }
}
Upvotes: 0
Views: 113
Reputation: 3620
i think you can use the ExexutorService to relize the function, last week , i read some book , since java 1.5 ,it is the best answer.
public class LiftOff implements Runnable {
protected int countDown = 10;
private static int taskCount = 0;
private final id = taskCount++;
public LiftOff(){}
public LiftOff(int counDown) {
this.countDown = countDown;
}
public String status() {
return "#" + id + "(" + (countDown >0 ? countDown : "LiftOff!") + ")";
}
public void run() {
while(countDown-- > 0) {
System.out.pritln(status);
Thread.yield();
}
}
}
public class Test {
public static void main(Strig args[]) {
ExectorService execu = Exector.newCachedThreadPool();
for(int i = 0; i++) {
execu.execute(new LiftOff());
execu.shutdown();
}
}
}
Upvotes: 0
Reputation: 96454
Thread and Runnable are 2 different things:
A Thread is an object that maps to an OS thread. Calling start on a Thread allocates and executes a thread.
A Runnable describes a task to be performed.
A Thread is only one way to perform a Runnable. You can run a Runnable either using a Thread, as in
Runnable myRunnable = new Runnable() {
public void run() {
System.out.println("Hello");
}
};
new Thread(myRunnable).start();
or you can submit a Runnable to an ExecutorService and let the service decide how to execute it:
executorService.submit(myRunnable);
or you can execute the Runnable in the current thread:
myRunnable.run();
As a convenience someone decided to make Thread implement Runnable, possibly so they could write demos with slightly less code.
Upvotes: 1
Reputation: 68715
One of the Thread class constructor accepts Runnable instance as mentioned here:
public Thread(Runnable target)
Allocates a new Thread object. This constructor has the same effect as Thread (null, target, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.
Parameters: target - the object whose run method is invoked when this thread is started. If null, this classes run method does nothing.
So do like this:
Thread t = new Thread(greetings2);
t.start();
When the thread is started it will kick of your NamedRunnable.run
method.
Upvotes: 1
Reputation: 11030
Pass a Runnable
to a Thread
to run it, or use another class like ExecutorService
.
new Thread( greetings2 ).start();
BTW, this is probably a terrible idea:
public class Threads extends Thread{
//...
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from thread ’" + name + "’!");
}
All sorts of confusion can result from overriding methods like run()
and start()
. You change their semantics (i.e., what they do) completely when you do this. Use Thread
without sub-classing it or use classes like ExecutorService
. Sub-classing Thread
is like so last century.
Upvotes: 1