File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where is main's run()

 
Pingling Chen
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that calling run() directly does not actually start a new thread, whereas calling start() does.
 
Pingling Chen
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic