hi folks, could you please explain me what is the difference between Process and Thread? i would appreciate if you could provide an example as well. Threads shared the same address space. what does that mean? Many processes may be associated with one and the same program what does that mean? what's the difference between Process and Program. how could a program have only one process? thanks.
You surely understand the concept of a "process" on your computer. When you launch a program, the result is a process; a running instance of a program. You can launch notepad.exe three times; there's one notepad.exe program -- i.e., one file named notepad.exe on your disk -- but three notepad processes. The details differ across operating systems, but typically a process has its own stack, its own virtual address space, its own set of file descriptors, etc. Each process can more or less pretend that its the only process running on that computer; each process is fairly independent. If I create an object in one process, I can't readily pass it to another process, because each process has its own address space. A memory address that is meaningful for one process may not be meaningful for another; a pointer created in one process won't work in another process. Also, each process believes that its instructions are executed one after the other, in order, without interruption. The operating system distributes the available CPU time among all the running processes. Threads are sometimes called "lightweight processes." Again, the details vary slightly across operating systems, but in general, a thread is like a process, except it doesn't get its very own address space, file descriptor table, etc. It does get its own stack, but everything else it has to share with other threads. Invariably a thread "belongs to" a process; each process has one or more threads in it. Because the threads in a process share an address space, they can readily pass objects between themselves. Make sense?
thanks Ernest. I also read that The start() method is invoked on the thread object.However, the start() methods returns immediately after thread has been spawned. what does that the start() methods returns immediately after thread has been spawned. mean? could please explain me? thanks.
A Thread object is just a plain ordinary old object. The start() method is magic, though. It creates a separate thread (in the sense that I was talking about in my last message) and sends it off to run on its own. The start() method then returns. The separate thread then calls the run() method of the Thread object (which may in turn call run() on another object that implements the Runnable interface.) Before you call start(), there are N threads; after start() returns, there are N+1 . The new thread is still running; start() doesn't wait for the other thread to terminate. That's what the text you're quoting means.
thanks Ernest. take a look at following code and give explanation on the way it works.
i expected to see the output as follows: Thread[Counter A, 5, main] Inside in run Counter A: 0 Counter A: 1 After counterA! Main Thread: 0 ... ... ... i am wondering howcome worker.start() does not go ahead and do run() but returns to main()method and prints "After counter!". could you please explain me this? thanks.
Well, that's what I was trying to explain. start() returns right away. Once start() returns, there are two separate threads running: the original one, which goes on to print the "After CounterA" message, and the new one, which goes on to call run() and print "Inside in run", etc. These threads are separate and unsynchronized. They act as if they are each running on their own CPU -- i.e., as if they were both running at the same time. If both threads print messages, as here, you'll see the messages all mixed up together, and the order can change each time you run the program.