Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A bunch of thread questions!

 
Bob Graffagnino
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying for the SCJP exam (I'm taking it 2/8/02) and have some thread questions.
QUESTION 1:
Can threads execute concurently (at the exact same nano-second)?
-or-
Do threads take turns executing?
-or-
Does it depend on the OS?

QUESTION 2:
Does a call to a thread object's yield method relinquish an object lock if it is called while executing an object's synchronized method?
Answer: Okay I got this one on my own. The Java API documentation indicates that yield() does not release a lock on an object.

QUESTION 3:
Does Thread.sleep(i) cause the current executing thread to relinquish a lock on an object if the thread happens to be executing an object's synchronized method?
Answer: I got this one as well. The Java API documentation indicates that sleep() does not release a lock on an object.
QUESTION 4:
While a thread is executing an object's synchronized method it goes into a blocked state. Does the thread give up its lock on the object it was using?
QUESTION 5:
Does a thread always leave the running state when it performs an IO operation like:
QUESTION 6:
Let's say an object's synchronized method calls wait() but never calls notify() or notifyAll(). Also assume that the threads that use this object are non-daemon threads. Would this cause the program to run forever?
Thanks!
Bob
[ January 30, 2002: Message edited by: Bob Graffagnino ]
[ January 30, 2002: Message edited by: Bob Graffagnino ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ask one question per thread it makes it a lot easier to answer you!

QUESTION 1:
Can threads execute concurently (at the exact same nano-second)?
-or-
Do threads take turns executing?
-or-
Does it depend on the OS?

It depends on the JVM, the OS, and the hardware. You can't be sure. But, IF you are on a multi-processor system and the OS and JVM support it, you could have two threads running at the exact same time, yes.
Rob
 
Bob Graffagnino
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I considered creating seperate posts for each question, but I didn't want to hog browser real-estate.
Regarding your answer to question 1: What if the program was running on a machine with only one processor?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well since ultimately the CPU is what runs a thread/process, it would be really really hard for a single CPU to run two threads at the exact same time. Really hard. Like, impossibly hard.

Rob
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
QUESTION 4:
While a thread is executing an object's synchronized method it goes into a blocked state. Does the thread give up its lock on the object it was using?

The thread does not go into a blocked state if it successfully obtained the lock. If it got the lock, it starts executing the method or code block. It only blocks if it tries to get a lock on an object when another thread already has a lock.
Rob
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
Well since ultimately the CPU is what runs a thread/process, it would be really really hard for a single CPU to run two threads at the exact same time. Really hard. Like, impossibly hard.
Well, not nearly as hard as you might think, Rob Look here for more information.
- Peter
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe, ok Peter.
But I don't think that Hyper-Threading will be on the SCJP test.

Rob
[ January 31, 2002: Message edited by: Rob Ross ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Two threads can not operate silumtaneously on the same CPU. It does not matter the OS. They always switch.
2. No.
3. No.
4. No.
5. No, if I understand what "running state" is.
6. Strange question since once you call wait() you can not do anything until someone else calls notify on you. So if nobody ELSE calls notify on you, yes the program is not going to exit.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:
1. Two threads can not operate silumtaneously on the same CPU. It does not matter the OS. They always switch.
Given the question - which does not give any hint regarding the hardware environment - the answer is surely "yes, they can execute the same nano-second". The point of the question probably is that, when developing threaded code, you must assume that some or all of your threads may execute truly simultaneously.
The OS may well enter the equation in multi-CPU systems; a dual-CPU box running WindowsMe will not run more than one thread at a time, the same box running WindowsNT will.
By the by, these days even a single CPU may run more than one thread at a time. You may want to look into hyperthreading. A really neat idea if you ask me.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic