Forums Register Login

Where is main's run()

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Also note that calling run() directly does not actually start a new thread, whereas calling start() does.
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
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://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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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://coderanch.com/how-to/java/ThreadsAndSynchronizationFaq.
If you're gonna buy things, buy this thing and I get a fat kickback:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 863 times.
Similar Threads
join() start() run()
Thread question
Threads Question
Can we only start() a thread?
Regarding the IllegalThreadStateException
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:42:10.