Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes How are threads implemented in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How are threads implemented in Java" Watch "How are threads implemented in Java" New topic
Author

How are threads implemented in Java

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Dear friends,
Can anyone please tell how threads are implemented in java. In c++ they are implemented by the fork and such sort of things. But what is the underlying implementation of threads in java. Anyone please let me know.
Thanks in advance


Thanks,
Shekar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There's actually no such thing as "C++ threads," and no version of threads are implemented via fork() (the system call that creates a whole new process on UNIX.)
On most systems, threads are an OS-level concept, and high-level languages merely add an interface to the underlying OS's implementation. Java is no exception: the low-level implementation of Java threads almost always involves mapping Java threads onto OS threads (except on platforms where threads are not a standard feature, in which case Java's thread support is implemented on top of a user-space thread library.)


[Jess in Action][AskingGoodQuestions]
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Thanks ernest,
but can you be a little bit clear about that.
Can I know how atleast at the OS level(very briefly) how threads are implemented. and how they are implemented at the top level using the user level library in certain standard systems which do not support threads.
a little brief description is needed.
Thanks a lot in any case
shekar.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
and also ernest,
i forgot to ask you that you said in your answer that "java threads map to OS threads". What exaclty are those java threads???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

At the OS level, threads are just a mechanism for sharing a CPU between multiple instruction streams by executing a few instructions from one stream, then a few from another, then a few from another. There is a thread scheduler, just like there's a process scheduler. The main difference between threads and processes (on most OSs, anyway) is that a process gets its very own address space, while multiple threads can share the address space of a single process. This makes switching between threads faster than switching between processes, because there is less involved. Each thread gets its own stack within that single address space.
The implementation will be more or less the same whether it's in the OS kernel or in a user library.
What's a Java thread? It's an instance of the class java.lang.Thread.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Thanks ernest,
thanks a lot. I got it.
A small request. I have posted few requests in other disciplines. If you dont mind I want you to answer questions to them. somehow I feel that you give very good answers. this is not to offend anyone but most of the questions which I have put on the forum have been answered by you.
Thanks once again.
chandrakant dhutadmal
Greenhorn

Joined: Feb 29, 2004
Posts: 1
There are basically two ways to create threads in Java:
1) extend Thread class provided in java
2) implement Runnable interface.
U use runnable in case, if u have a class which extends from some other class and that the extended class needs to be a thread. otherwise simply extending the Thread class will generally suffice.
eg.

class ThreadDemo extends Thread
{

public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(i);
}
}
}
public class ThreadDemoImplement
{
//create teo threads.
ThreadDemo t1=new ThreadDemo();
ThreadDemo t2=new ThreadDemo();
t1.start();
t2.start();
}
try this out and lemme know hows that.
 
wood burning stoves
 
subject: How are threads implemented in Java
 
Similar Threads
Collections in java
File Upload Using Servlets
Concurrent Programming & Multi-threading book recommendation
Design Patterns used in JAVA threads
threads