Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- By calling t1.start(),
- we are getting the main calling thread returned immediately
- after the t1.start() called and is ready to proceed for other
- operations.And the thread t1 starts executing the run method of the object r.
- Hence the the output will be:
- I am the main thread , i created thread t1 and had it execute run method, which is currently looping from 0 to 1000000
- I am done executing run method of testThread
- */
- /* If we call t1.run() instead of t1.start(), (just replace t1.start() with t1.run() in the code for testing)
- its like a regular method call and the main thread will not return until the run method completes,
- hence the output will be:
- I am done executing run method of testThread
- I am the main thread , i created thread t1 and had it execute run method, which is currently looping for i to 1000000
- */
- class testThread implements Runnable{
- public void run()
- {
- for(int i=0;i<1000000;i++){} //a simple delay block to clarify.
- System.out.println("I am done executing run method of testThread");
- }
- }
- public class mainClass{
- public static void main(String [] args)
- {
- testThread r = new testThread();
- Thread t1 = new Thread(r);
- t1.start(); /* Question is: can we call instead t1.run() */
- System.out.println("I am the main thread , i created thread t1 and had it execute run method, which is currently looping for i to 1000000");
- }
- }
- public synchronized void start() {
- /**
- * This method is not invoked for the main method thread or "system"
- * group threads created/set up by the VM. Any new functionality added
- * to this method in the future may have to also be added to the VM.
- *
- * A zero status value corresponds to state "NEW".
- */
- if (threadStatus != 0)
- throw new IllegalThreadStateException();
- group.add(this);
- start0();
- if (stopBeforeStart) {
- stop0(throwableFromStop);
- }
- }
- private native void start0();
Add Comment
Please, Sign In to add comment