This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Where is main's run() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Where is main Watch "Where is main New topic
Author

Where is main's run()

Pingling Chen
Greenhorn

Joined: Jan 26, 2013
Posts: 4
For given Java code as follows,
----------------------------------------------------
public class RunOfMain{
public static void main(String[] args){
Thread t=Thread.currentThread();
System.out.println("--- RunOfMain --- start");
System.out.println("currentThread: "+t);
System.out.println("..before t.run()");
t.run();
System.out.println("..after t.run()");
System.out.println("..before t.start()");
t.start();
System.out.println("..after t.start()");
System.out.println("--- RunOfMain --- end");
}
}
---------------------------------------------
C:\java2013\0127>javac RunOfMain.java

C:\java2013\0127>java RunOfMain
--- RunOfMain --- start
currentThread: Thread[main,5,main]
..before t.run()
..after t.run()
..before t.start()
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:682)
at RunOfMain.main(RunOfMain.java:10)
-------------------------------------------------
According to result, I assume there is a run() method of this program.
Where can I find further spec or documentation to explain this phenomena?
Thanks!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Look up the Thread class.

You access the current thread and fiddle around with it. The runtime environment will run your program in a thread, the main one. The thread will have a run() method, as every thread has, but since the main thread has been started, it is running (otherwise it could not execute your main method), it will not be possible to start it again, hence the IllegalThreadStateException. A thread can be started only once.

Besides playing around and testing, I do not see much sense in the sample program.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
Also note that calling run() directly does not actually start a new thread, whereas calling start() does.


Ping & DNS - my free Android networking tools app
Pingling Chen
Greenhorn

Joined: Jan 26, 2013
Posts: 4
Thank you for your kind replies.

This test is designed to verify my understanding of main thread behavior, which is not straightforward to me comparing to class extends Thread.

IllegalThreadStateException is expected whenever compiler accepts t.start(), which also indicates there is a run().
My question was why there's a run()?

Happily I fould out the answer satisfied me from API, http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#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.

For this case, it applies "this method does nothing and returns".
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
The run() method is what you would implement using the Runnable interface (which is what you should do instead of extending thread). And yes, calling start() will eventually lead to run() being called by the JVM.

See https://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for some more discussion, and also links to some more powerful classes you might want to use instead, namely Callable and Future.
Pingling Chen
Greenhorn

Joined: Jan 26, 2013
Posts: 4
Thank you, Ulf.

I have enough understanding of the link you mentioned.

But Executor is still not clear to me.
http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html, is not too helpful at this moment.
Any advice for further reading, as possible something like http://www.javaranch.com/campfire/StoryPassBy.jsp
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
If you're really interested in learning Java concurrency properly (and these being the days of multi-core CPUs, multi-CPU servers and multi-server applications, I don't see how anyone could not be), work through one of the eminent books on the subject; they're listed in the https://www.coderanch.com/how-to/java/ThreadsAndSynchronizationFaq.
 
wood burning stoves
 
subject: Where is main's run()
 
Similar Threads
Threads Question
join() start() run()
Can we only start() a thread?
Thread question
Regarding the IllegalThreadStateException