• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Threads in Java VS Threads in Application Server/Tomcat

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have been struggling with this question since many days. I have googled lot about this, but couldn't find the answer. So posting it here, hoping someone can help me  

From the Head First Java book, I know that threads in java doesn't run parallelly, but JVM just switches between the threads which seem to us like running parallelly. And these threads run in a single JVM which itself a process that runs in one core of the Computer.

My doubt is, does the tomcat threads and application server threads which are created per user request are also not run parallelly but JVM which runs on a single core (on which application server runs) just switches between the threads?
OR does threads really run parallelly on different cores?

By the way I am new to ranch, Please correct me if I misunderstood something wrong  

Thank you.
 
Saloon Keeper
Posts: 13366
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Can you please quote your sources? I can't imagine that Head First says that threads don't run in parallel.

It's true that Java has its own notion of threads (so-called "green threads") that are separate from threads provided by the operating system, and that two green threads may be executed by the same OS thread, which means they won't run at the same time.

This does NOT mean that two green threads will never be run in separate OS threads by separate processor cores. I'm certain that Java strives to use multiple processor cores at the same time whenever possible.

Application containers like Tomcat aren't special. They're just regular Java applications that run on the JVM and have the same limitations as any other Java application. When an application container requests a green thread from the JVM, the JVM will probably try to schedule it in such a way that it makes use of an idle core when available.
 
vishnu priyag
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch!

Can you please quote your sources? I can't imagine that Head First says that threads don't run in parallel.

It's true that Java has its own notion of threads (so-called "green threads") that are separate from threads provided by the operating system, and that two green threads may be executed by the same OS thread, which means they won't run at the same time.

This does NOT mean that two green threads will never be run in separate OS threads by separate processor cores. I'm certain that Java strives to use multiple processor cores at the same time whenever possible.

Application containers like Tomcat aren't special. They're just regular Java applications that run on the JVM and have the same limitations as any other Java application. When an application container requests a green thread from the JVM, the JVM will probably try to schedule it in such a way that it makes use of an idle core when available.




Hi,
Thanks for the reply. This is the portion of the information that i took from the book.

" With more than one call stack, you get the appearance of having multiple things happen at the same time. In reality, only a true multiprocessor system can actually do more than one thing at a time, but with Java threads, it can appear that you’re doing several things simultaneously. In other words, execution can move back and forth between stacks so rapidly that you feel as though all stacks are executing at the same time. Remember, Java is just a process running on your underlying OS. So first, Java itself has to be ‘the currently executing process’ on the OS.

Typically, a thread moves back and forth between runnable and running, as the JVM thread scheduler selects a thread to run and then kicks it back out so another thread gets a chance.  "

This tells that the thread scheduler is selecting the thread that needs to be run currently and then after sometime, it makes other thread to run and then it again makes first thread takes it part to run.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:. . .  only a true multiprocessor system can actually do more than one thing at a time . . .

That shows HFJ's age. When it was printed in 2005 (2nd edition), multiprocessor systems were rare. Now sixteen years later, almost every chip you can buy contains multiple processors.
 
vishnu priyag
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

vishnu priyag wrote:. . .  only a true multiprocessor system can actually do more than one thing at a time . . .

That shows HFJ's age. When it was printed in 2005 (2nd edition), multiprocessor systems were rare. Now sixteen years later, almost every chip you can buy contains multiple processors.



Hi,

So, in current time, every thread in JVM runs parallelly in different cores?
Is this same case for tomcat threads and application server threads that are created per request?
Is there any difference between them?
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:. . . So, in current time, every thread in JVM runs parallelly in different cores?

No. That would be impossible if there are more threads to run than there are cores/threads to execut them on, or if some other process requires a core for itself.

Is this same case for tomcat threads and application server threads that are created per request?
Is there any difference between them?

Don't know.
 
vishnu priyag
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No. That would be impossible if there are more threads to run than there are cores/threads to execut them on, or if some other process requires a core for itself.



Ok. Thank you.

Can anyone please clear above 2 questions:
Is this same case for tomcat threads and application server threads that are created per request?
Is there any difference between them?
 
Stephan van Hulst
Saloon Keeper
Posts: 13366
295
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would Tomcat be any different? Like I said, Tomcat is a normal Java application.
reply
    Bookmark Topic Watch Topic
  • New Topic