aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What exactly are java threads?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What exactly are java threads??" Watch "What exactly are java threads??" New topic
Author

What exactly are java threads??

abhishek shanker
Greenhorn

Joined: Dec 18, 2012
Posts: 7


When we say "java threads are mapped to the system level threads by JVM/OS" Do we mean that when this java thread executes, then it is actually OS thread that is executing the java code?
abhishek shanker
Greenhorn

Joined: Dec 18, 2012
Posts: 7
No one knows answer to this question in java ranch ???
Deepak Kumar Jha
Greenhorn

Joined: Feb 07, 2013
Posts: 17

can you explain your question in more detail. @abhishek
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 796
    
    1
abhishek shanker wrote:

When we say "java threads are mapped to the system level threads by JVM/OS" Do we mean that when this java thread executes, then it is actually OS thread that is executing the java code?


Threads are processes that execute concurrently.
If you have one CPU in your computer and three threads are created. The three threads take it in turns to execute. For example, thread 1 executes for 0.1 second, then thread 2 executes for another 0.1 second, then thread 3 executes for another 0.1 second. Then, thread 1 takes its turn and so on.

If you have two CPU's, two threads may be assigned to one CPU and the other thread may be assigned to one CPU. This is just an example.

With multiple thread processing, threads take turn to execute (time share the CPU and hardware resources), so that no one thread waits forever until other threads finish.
Sai Krishna Haridass
Greenhorn

Joined: Nov 13, 2010
Posts: 4
@abhishek - I am not sure what level you are at, so I will explain the basic concept here, then give an example and then dive a little deeper.

Concept: You will create threads out of objects of class which either implements Runnable or extends Thread.

Please look at my simple thread example.

[code=java][
//Below is a simple clas with name Human

public class Human implements Runnable {//extends Thread {

String name;
public Human(String name) {
this.name = name;
System.out.println("Hi I am: "+ name + "'s thread");
}

public void run(){

System.out.println("I("+ this.name +") will run now since OS wants me to run now:");

for(int i = 0; i<1000000; i++){

System.out.println(this.name + " Step number: "+ i);


}

}
/*
* To Demo how multiple threads can be spun on multiple object at same time
*/
public class Test1 {

public static void main(String[] args) {

String[] names = {"sai","hari","lazyguy","ram","sri"};

for(String s: names){
Human name = new Human(s);
Thread namethread = new Thread(name);
namethread.start();
}

System.out.println("Already out of for loop...");
}

}
]

In the above example threads are created when you say Thread namethread = new Thread(name). They become runnable when you say namethread.start().
At this point your thread is not running yet,but it is eligible for running. Os which has its own threads schedules this thread as per some predefined rules (depends on os design). Once your thread is runnable the OS thread will run your thread with the help of its own threads, this what it means when you say java threads are mapped to the system level threads.



abhishek shanker
Greenhorn

Joined: Dec 18, 2012
Posts: 7
@haridass : thanks a lot for your reply

BTW, lines which i was looking for were

"OS which has its own threads schedules this thread as per some predefined rules (depends on os design). Once your thread is runnable the OS thread will run your thread with the help of its own threads, this what it means when you say java threads are mapped to the system level threads. "

Which means, at the end it is the OS level threads which executes on behalf of java thread. Correct ??
Sai Krishna Haridass
Greenhorn

Joined: Nov 13, 2010
Posts: 4
Yes and No.

Yes because OS thread runs your thread as per its schedule.

No because it does not perform the role of your thread it just runs the thread to let it do its job ( which you would define in the call or run method of the object).

Please let me know if you have any other questions
Sai Krishna Haridass
Greenhorn

Joined: Nov 13, 2010
Posts: 4
I would like to give you a small analogy, which might help. Lets say you want to make some Milk shake. You will use blender.

blender -- thread
Your hand -- OS thread

when ever you want you will press the blender power button and the blender blends the contents to make a milk shake.
Sai Krishna Haridass
Greenhorn

Joined: Nov 13, 2010
Posts: 4
But at the end of the day, thread is still an object, which you might have noticed
abhishek shanker
Greenhorn

Joined: Dec 18, 2012
Posts: 7
@Haridas: Thanks a lot , good analogy, now i understood better
abhishek shanker
Greenhorn

Joined: Dec 18, 2012
Posts: 7
@Himai: thanks for you reply ,
i know what what threads are and their usage ( from OS viewpoint)
but was more interested in mapping between java and OS thread ( like .. how it is done )

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What exactly are java threads??