Process and Thread

Reading Time: < 1 minute

Process

A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process, e.g. memory and CPU time, are allocated to it via the operating system.

Thread

Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process. Threads exist within a process — every process has at least one. Threads share the process’s resources, including memory and open files. This makes for efficient, but potentially problematic, communication.

Defining and Starting a Thread

By implementing the Runnable Interface

public class HelloRunnable implements Runnable
{
	public void run()
	{
		System.out.println("Hello from a thread!");
	}

	public static void main(String args[])
	{
		(new Thread(new HelloRunnable())).start();
	}
}

By extending the Thread class

public class HelloThread extends Thread
{
	@Override
	public void run()
	{
		System.out.println("Hello from a thread!");
	}

	public static void main(final String args[])
	{
		(new HelloThread()).start();
	}
}