Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How are threads implemented in Java

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic