package com.jspiders.thread;
public class Mthrd1 extends Thread {
{
#Override
//error line public void run()
{
// TODO Auto-generated method stub
super.run();
}
}
}
This is a valid way to override the run method for creating threads:
class Test extends Thread
{
public void run()
{
System.out.println("Thread Started and is Running");
}
public static void main(String args[])
{
Test t=new Test();
t.start();
}
}
There is also another way to create threads in java by implementing the runnable interface. In you posted code you are calling the run method explicitly whereas, for calling the run method of Thread class you need to call start() method which implicitly calls the run method of Thread, your program is showing error because as you are calling run method explicitly and in your class you have no method called run.
Related
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
System.out.println("hello");
}
})
{
public void run() {
System.out.println("world");
}
}.start();
}
Simply I get "world" with this. Why it can be written in this manner? What is the first run() method for?
This (contrived) code passes an instance of an anonymous Runnable class to an instance of an anonymous Thread class that overrides its usual implementation of run().
It's becomes (a little) clearer if you refactor it:
// The runnable instance
Runnable runnable = new Runnable() {
public void run() {
System.out.println("hello");
}
};
// Anonymous Thread class with a custom run() method
new Thread(runnable) {
public void run() {
System.out.println("world");
}
}.start();
The Runnable passed to the Thread, which would be executed by the thread's start() method, is ignored because the Thread object is also an instance of an anonymous class with a custom implementation of its run() method, which is hard-coded to print "world".
This code creates an instance of an anonymous sub-class of Thread and then starts that thread.
That sub-class overrides Thread's run() method with a method that prints "world".
The first run() method is part of an anonymous implementation of the Runnable interface which is passed to the constructor of the Thread instance. That implementation is ignored, since your Thread implementation overrides run().
The Runnable instance passed to the thread constructor is only executed (when the thread starts) if the Thread's run() method is not overridden.
The Thread class defines run() in this way :
#Override
public void run() {
if (target != null) {
target.run();
}
}
It invokes the run() method of the Runnable instance (target field of Thread)if this field is not null.
So when you create an anonymous class of Thread that overrides this method in this way:
new Thread(...) {
#Override
public void run() {
System.out.println("world");
}
}
The Thread run() method of the anonymous instance doesn't rely any longer on the target field to run the Runnable instance.
It execute only this code :
System.out.println("world");
So the Runnable instance passed to the constructor of Thread is totally ignored by run():
new Runnable() {
public void run() {
System.out.println("hello");
}
}
You have overridden the run method which prints hello with the one which prints world. This happened because in your code you have extended the Thread class with an anonymous class which overrode the run method, therefore the Runnable parameter's run is never called. You can update the code to use super.run() to call the runnable method from the original class.
Here is your code with the changes:
new Thread(new Runnable() {
public void run() {
System.out.println("hello");
}
}) {
public void run() {
super.run();
System.out.println("world");
}
}.start();
I wonder if the following code is OK:
public class MyClass {
...
static {
new MyClass().start();
}
private void start() {
//running the thread
}
}
I must add, I don't have access to something like main method, because this part of code is called from outside.
Does exists more sophisticated way of starting this thread?
I'm slowly transitioning from C++ to java and I do not understand the following piece of code:
public class TestThread
{
public static void main (String [] args)
{
Thread t = new MyThreads()
{
public void run()
{
System.out.println(" foo");
}
};
t.start();
System.out.println("out of run");
}
}
An object type "MyThreads" is being instantiated, but what does the function "void run" mean?
Why is it written using that syntax right after the object instantiation?
Is that function being overriden?
When is this kind of syntax (where I define a function with an object instantiation) necessary/required? and where is it preferred/useful?
It means that the class MyThreads either require you to first write a method with name run or the way you are doing provides the ability to change the existing run method behaviour where you are declaring.
It is like overriding if run method is already there or creating the method when you want to create object.
This provides the ability to create objects of MyThreads without having to change the original class or creating multiple classes.
public class TestThread
{
public static void main (String [] args)
{
Thread t = new MyThreads()
{
public void run()
{
System.out.println(" foo");
}
};
t.start();
Thread t1 = new MyThreads()
{
public void run()
{
System.out.println(" this time it is somethidn else");
}
};
t1.start();
System.out.println("out of run");
}
}
Little modification to your code shows the advantage of having this feature. If you observe run method of t1 is doing something different than what is in t. So it is now completely new thread.
This code is equivalent to
public class TestThread
{
static class MyThreadSubclass extends MyClass {
public void run() {
System.out.println("foo");
}
}
public static void main (String [] args)
{
Thread t = new MyThreadSubclass();
t.start();
System.out.println("out of run");
}
}
It's just a convenient way of defining a subclass inline, without having to give it a name; it's just syntactic sugar. It's creating an object of a subclass that overrides the method run() from MyThreads.
I am using a java library which requires certain objects to be used from the AWT Event Dispatch Thread. I would like to create an interface for a program using this library. So far I have this.
public abstract class IProgram implements Runnable {
public static void main(String[] args) {
}
public void run() {
start();
}
public abstract void start();
}
In the main method I would like to run whatever subclass is made. For example, if this was the program:
public class MyProgram implements IProgram {
public void start() {
//Code to run program here...
}
}
I would like to then run MyProgram and have it run as a runnable. Is this possible? If it is, would I put in the main method of IProgram?
Edit: normally to call a runnable I use SwingUtilities.invokeLater(new RunnableImplementation());
The Java docs state that if we supplied a Runnable target when creating a new thread, .start() of that thread would run the run() method of the supplied runnable.
If that's the case, shouldn't this test code prints "a" (instead of printing "b") ?
public class test {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
System.out.println("a");
}
};
Thread t = new Thread(r) {
#Override
public void run() {
System.out.println("b");
}
};
t.start();
}
}
Because you are overriding Thread.run() method.
Here is the implementation of Thread.run():
#Override
public void run() {
if (target != null) {
target.run();
}
}
try:
}) {
#Override
public void run() {
super.run(); // ADD THIS LINE
System.out.println("b");
}
}.start();
You will get ab.
The default implementation is to call the Runnable. However you are overriding the default implementation and NOT calling the runnable. The simplest way to fix this is
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("a");
}
}) {
#Override
public void run() {
super.run(); // call the default implementation.
System.out.println("b");
}
}.start();
The default implementation of Thread.run() does what the javadocs say (look at the source code)
public void run() {
if (target != null) {
target.run();
}
}
However, Thread.run() is public, and you overrode it, so it calls your println("b"), and totally ignores the Runnable passed in the constructor. Arguably, Thread.run() should be final, but it isn't.
You have overridden the default implementation of Thread.run() in what I guess is an anonymous subclass, hence what the JavaDoc says no longer applies.
If you try
public class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("a");
}
}) .start();
}
}
You get the answer you expect, a.
The first block overrides run in Runnable. The second block overrides run in Thread. When you call start on a Thread, its run method is invoked. The default run method in Thread calls the Runnable.run method but since you overrode Thread.run there is no code to start the Runnable - only your code to print 'b'.
The documentation that I have says this:
"Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread."
which means that it should print b instead of a, since you have overridden the run() method of Thread.
You are calling start() method. Reading the docs in the link you provided it states:
public void start()
Causes this thread to begin execution; the Java
Virtual Machine calls the run method of this thread.
You should call the run() method if you would like to call the Runnable object's run method.
public void run() If this thread was constructed using a separate
Runnable run object, then that Runnable object's run method is called;
otherwise, this method does nothing and returns.
You are actually extending the Thread class and calling start on the instance of that anonymous subclass.
I think the confusion is that the "Java Doc" is for the java.lang.Thread class not for your class that extends that class.
e.g.
Runnable r = new Runnable() {
#Override
public void run() {
System.out.println("a");
}
};
Thread t = new Thread(r);
Now, if it does not call that run then java doc is wrong. Which is not true.