February 2nd, 2010

There are two ways of Thread creation either by implementing Runnable or extending the Thread class. In both the cases you need to start the thread for getting the functionality you need. So in order to start a thread we need a method which is start() and it is invoked on Thread object.

Remember:-start() method is always invoked on Thread object to start a separate call stack of this thread,but if you call the run() method on your Runnable object then it is simply a method call and cannot initiate a separate call stack. It also clears that You start a Thread not a Runnable.

To start a new Thread in separate call stack use

t. start() // where t is Thread object which is in execution.

We have made a call to start() method but know what happens behind the scenes there are three things happen:-

  1. A new Thread in execution starts(in a new call stack).
  2. The Thread moves from the new state to the runnable state.
  3. Whenever the Thread gets a chance to execute then it goes to running state and the target run() method will run.

Consider the following example to understand how Threads are started and also you will come to know that invoking run() method do works but it never ever initiate a new Thread or a separate call stack.

class TestRunnable implements Runnable

{

public void run()

{

for(int i=0;i<5;i++)

System. out. println(”In Runnable”);

}

public static void main(String a[])

{

Thread tr = Thread. currentThread();

System. out. println(tr);

TestRunnable r = new TestRunnable();

Thread t = new Thread(r);

System. out. println(r);

r. run();

System. out. println(t);

t. start();

}

}

OutPut:-

Thread[main,5,main]

TestRunnable@3e25a5

In Runnable

In Runnable

Thread[First Thread,5,main]

In Runnable

In Runnable

Explanations:-

Here in above example there are two things we wanted to get you know that new Threads are started by calling start() method on Thread object and Runnable can never ever start a new call stack. invoking run() method simply works as another method invocation. Also you can see that when we tried to print the names of Threads then r comes with only as object names whereas the Thread t and tr have original Thread names.

Comments

January 19th, 2010

In the previous tutorials about Threads we have learnt about the basics of Threads,How to create a Thread and also how to start a Thread. So far we have learnt how to start a single Thread but in this tutorial we will learn about how we can start and run Multiple Threads

To start Multiple Threads we use Runnable interface method which is best one. So we will implement the Runnable interface in our class and use the instance of our class to start Threads. To start a Thread we simply need a method start() to begin all the Threads. Read the rest of this entry »

January 5th, 2010

There are two types of variable mainly:-

1. Instance Variable or Class Variable
2. Local Variable or method variable

Instance variable are initialized by JVM to their default values if not defined explicitly. Whereas the local variables needs to be defined each time time they are declared. But the local variables can be used to a greater effect by using the concept of dynamic initialization. Read the rest of this entry »

December 22nd, 2009

Java is a common programming language and the basic purpose of Java is to develop network programs. Now it is also used to develop system software’s and programs too. The Java architecture has trade off between speed and efficiency. In case of Java the efficiency beats the speed the execution and rightly so because there are issues which are needed to be addressed in order to become a sound programming language. Read the rest of this entry »

December 8th, 2009

The sandbox allows code to be downloaded from any source,but applies restriction on it upon execution. You can also say sandbox isolates particular code during execution to apply the restrictions. A Java sandbox is an area in memory outside which the Java program cannot make calls. This prevents Java programs from being able to call low level system functions that may cause data corruption or other damages. The Java sandbox is used by Java to discourage unsafe applets from accessing the resources. It applies several restrictions on the applets.

Read the rest of this entry »

















Get Your Site
Submitted for Free
in the World's Largest
B2B Directory!

Email Address:
*URL:
*
*Indicates Mandatory Field
Terms & Conditions