Thread:
  • Thread is nothing but functionality which could be executed simultaneously with the other part of the program based on the concept of one with another.
  • But where as method or a function which would be executed hierarchically with the other part of the program.
  • It is termed as a ‘lightweight process’, since it is similar to a real process but executes within the context of a process and shares the same resources allotted to the process by the kernel
What is a process? difference between process and thread? 
  • A program which is under execution can be called as process.
  • Some operating systems use the term ‘task‘ to refer to a program that is being executed.
  • Thread is part of the process.
  • 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.
  • I'm not sure what "hardware" vs "software" threads might be referring to. Threads are an operating environment feature, rather than a CPU feature (though the CPU typically has operations that make threads efficient).
  • Threads share the address space of the process that created it; processes have their own address space.
  • Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  • Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  • Threads have almost no overhead; processes have considerable overhead.
  • A process is a collection of code, memory, data and other resources. A thread is a sequence of code that is executed within the scope of the process. You can (usually) have multiple threads executing concurrently within the same process.
How many ways to create thread in java: 
  • we can create threads in two ways.
  • 1. Extending Thread class.
    2. Implementing Runnable interface.

Extending Thread class:

package com.instanceofjavaforus;

public class ExtendsThread extends Thread {
 
    public void run(){
    System.out.println("thread is running...");
    }
    public static void main(String args[]){
    ExtendsThread t1=new ExtendsThread();
    t1.start();
     }
  
}

Implementing Runnable interface

package com.instanceofjavaforus;

public class RunnabelDemo  implements Runnable{
  
    public void run(){ 
    System.out.println("thread is running..."); 
    } 
    public static void main(String args[]){ 
    RunnabelDemo m1=new RunnabelDemo(); 
    Thread t1 =new Thread(m1); 
    t1.start(); 
     } 
   
}



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Multithreading

  1. Really a great post. I learned lots of things about Java.
    Java Training in Chennai

    ReplyDelete
  2. Nice post. I have been reading a lot of stuff on this topic in the last few months, but this article stands out with its simplicity & authenticity. Every passage made profound sense. Thanks a lot for this.

    ReplyDelete

Select Menu