Wednesday, June 13, 2012

JAVA THREADS

Process vs. threads

The distinction between processes and threads is important.

  • Process: A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process are allocated to it via the operating system, e.g. memory and CPU time.
  • Threads: threads are so called lightweight processes which have their own call stack but an access shared data. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data

    defining instantiating and starting thread by extending Thread class

    class mythread extends thread {
    public void run(){
    sop("hi satya")
    }

    class sample {
    p.s.v.main(string args[])
    {
    mythread th=new mythread() //instancing the thread
    th.start();
    sop("calling main thread ");
    }

    }









     

No comments:

Post a Comment