Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

RHE Mock exam

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When application A below is run, two threads are created and started. Each thread prints a message just before terminating. Which thread prints its message first?

t1 or t2
Here he says answer is t1. Because t1.sleep is static method so it will not have any effect on thread t1.
But what I feel is for example take static variable
class A{
static int t=10;
public static void main(String arg[]){
A a=new a();
a.t=20; //Now here the value of t is set to 20
}
}
When we call a.t the value is set to new value, so why not when we call t1.sleep method can have the same effect.
Also I want to know are the RHE mock exam are really good. What weightage it carries compare to real exam
ps:
Added the [ code ] [ / code ] tags on popular demand.
- satya
[ January 09, 2002: Message edited by: Madhav Lakkapragada ]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure if I understand your question.
Anyways, t1 sleeps only for 5 seconds whereas t2 sleeps for 10 seconds, so its only natural that t1's message would be printed first.
I don't quite understand your point on static thing. You might want to paraphrase it.
Lastly, consider using ubb codes.
When posting java code, enclose them with [ CODE ] and [ / CODE ], without the spaces
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example is perfect to show that t1.sleep doesn't act against t1 object, but against the current executing thread; that is t2. Because the sleep is static is not called on an instace. It is called on a class. What class? the declared type of the reference used in the call. Because t2 is of type Thread, Thread.sleep is called. This method, as API says, acts against the current thread.
Regarding "a.t=20;" a.t is not modifying an object of type A. It is accessing a static field of the class A, that is shared between all the instances.
I haven't taken the exam yet. I don't own RHE book, but the examples of it that people post look like very accurate.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I'm not clear on here:
is the contract with sleep() such that it *guarantees* that it will sleep for at least the time you have indicated in the argument? If that is true, then you can make a prediction like you are saying. If it's not true, then you can't make any deterministic assumptions about which thread prints first. It is *likely* that t1 always prints first, but not necessarily so.
Does anyone know if sleep(1000) is an iron-glad guarantee that a thread will sleep for at least a second?
Secondly, under what situations will sleep() throw an InterruptedException? This seems to be another place where it's possible that the timing of these threads is not going to be the same all the time for every platform.
Thoughts anyone??
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Secondly, under what situations will sleep() throw an InterruptedException?


From Java: How to Program(3rd ed.) by Deitel and Deitel pg 742-743
"Note that the sleep method can throw a checked InterruptedException (if another thread invokes the sleeping thread's interrupt method), therefore sleep must be called in a try block.
HTH,
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep, if not interrupted, is guarranted to sleep the exact time. However the exact time depends on the accuracy of the clock of the system. Also because the elapsed time had run out the thread can not be said to be already running. Only it becames eligable for the scheduler to choose it for running. Thus the time when a thread is running after a call to sleep is at least the argument to the method, but maybe more.
[ January 09, 2002: Message edited by: Jose Botella ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose,
what u pointed out is right. but as u said thread 'll sleep atleast for specified amount before run next. so, it can happen that system is loaded and thread with 5 second sleep gets delayed and thread with 10 second delay can be executed. we never know the exact output as Ross suggested. yeah, but its "likely" that thread with 5 second gets CPU hold first.
also, doesnt matter even if we start t1 or t2 earlier as start() doesnt guarantee the calling of run() immediately. so, t1 actually may get started so late that by the time t2 was finished.
i guess, this kind of questions shd not be asked and if they r asked it shd specify keywords like "which thread is MOST likely"...etc..
regards
maulin.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin
I agree with you.
Now a bit of javaFiction , because I don't really now for certain what it follows:
In a system, whether loaded or not, sleeping threads are not executed. In a loaded system the scheduler is still running and taking into account the time that goes off for the sleeping threads. When time has run out the sheduler changes the state of the sleeping threads, but they won't run because the system is very loaded. When the system is again able to run the two threads, any could execute because they have the same priority.
However, 5 seconds is a long time for a thread. Also the question is supposed to mean a normal enviroment.
mmm. a system frozen for 5 seconds! surely you are running the last version of Windows
[ January 09, 2002: Message edited by: Jose Botella ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that the static Thread.sleep() is finally a native method which deals with the current thread only but not object related. So even you use t1.sleep(10000), it will cause t2 which is the current thread to sleep at least 10 seconds.
Zheng Xu
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!