-
Notifications
You must be signed in to change notification settings - Fork 0
threads
Process An executing instance of a program is called process. Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
Thread A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.
Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces. Threads and process have their separate private run time stack but all threads see the same dynamic memory heap. *Stackoverflow
Multitasking refers to computer's ability to perform multiple jobs concurrently. Thread is a sequence of execution within a program. Multithread refers to multiple threads of control within a single program.(eg chrome tabs - thread. Chrome is process. Threads are a sub part of process)
- Maintain responsiveness of application
- Cancellation of sub tasks without crashing the process
- Parallel execution
- Monitor status of some resources
- Some API and systems demand it eg Swing
- Sub classing the Thread class and instantiating a new object of that class
- Implementing the Runnable interface
In both cases run() method should be implemented
`package demo1; class MyClass extends Thread {
public void run() { for (int i = 0; i < 10; i++) { System.out.println("value "+i); } } }
public class demo{ public static void main(String[] args) { MyClass myClass = new MyClass(); myClass.start(); } }
//+++++++++++++++++++++++++++ package demo1; class MyClass extends Thread {
public void run() { for (int i = 0; i < 10; i++) { System.out.println("Thread: " +Thread.currentThread().getId()+" value "+i); } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class demo{ public static void main(String[] args) { MyClass myClass = new MyClass(); myClass.start(); MyClass myClass1 = new MyClass(); myClass1.start(); } }
//++++++++++++++++++++++ `