aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green's exam(q.no 22) Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Watch "Marcus Green New topic
Author

Marcus Green's exam(q.no 22) Thread

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
This is the question
Question 22)
What can cause a thread to stop executing?
1) The program exits via a call to System.exit(0);
2) Another thread is given a higher priority
3) A call to the thread's stop method.
4) A call to the halt method of the Thread class
Answer given is 1, 2
Can we be sure that if we give higher priority to another thread,
that the current thread will stop exeuting.
When I think I am ok with threads, some times I am
Thanks,
Vanitha.

Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
The rather nice answer to this question on my mock exam got somewhat scrambled for no reason I can work out. Here is what it now says after de-scrambling...

"Note that this question asks what can cause a thread to stop executing, not what will cause a thread to stop executing. Java threads are somewhat platform dependent and you should be carefull when making assumptions about Thread priorities. On some platforms you may find that a Thread with higher priorities gets to "hog" the processor.
You can read up on this in more detail at http://java.sun.com/docs/books/tutorial/essential/threads/priority.html
http://www.jchq.net/tutorial/07_01Tut.htm
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hey even i got confused !
I tried following and found that even for the highest priority thread(Priority = 10) , the scheduler does not stop scheduling low Priority thread.
CODE-------
class th1extends Thread
//class FThreadimplements Runnable
{
public void run()
{
while (true){
System.out.println( Thread.currentThread().getName() + " running!" );
try{
Thread.sleep( 1000 );
}
catch ( InterruptedException e ){}
}
}
}
public class Pri{
public static void main( String[] args ){
//Thread my = new Thread( new FThread() );
th1 my = new th1();
my.setName("my");
my.setPriority(2);
my.start();
try{
Thread.sleep( 1000 );
}
catch ( InterruptedException e ){}

th1 t1 = new th1();
t1.setPriority(10);
t1.setName("t1");
t1.start();
}
}
The OUTPUT i got is...
my running!
t1 running!
my running!
t1 running!...
I use win98 and I think Win98, being a time sliced system, it intterupts the higher Priority thread for a lower one.
So the option should be wrong as it depends on OS.
Correct me if i am wrong!
Rashmi
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
Vanitha
From "The Java Tutorial"

The chosen thread will run until one of the following conditions is true:
A higher priority thread becomes runnable.
It yields, or its run method exits.
On systems that support time-slicing, its time allotment has expired.
Then the second thread is given a chance to run, and so on, until the interpreter exits.

All other things being the same, given two runnable threads the higher priority thread will run. I'm fairly certain that this is the case. Anyone got any other ideas?

Adam
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your reply Marcus and Rashmi. Have to be careful with can and will
Vanitha.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your reply Adam, take a look at Marcus's explanation.
Vanitha.
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
Futher to the point Rashmi has raised, I think Windows OS have 7 or so priority levels. However where the JVM would fit in I don't know, so couldn't say about time-slicing effects. I agree with Marcus though - the main point is that you can't put blind faith in Thread scheduling as it varies platform to platform and can be undermined in unpredictable ways. However, I did the same question and I have to say I did actually choose 2 as an option.
Adam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green's exam(q.no 22) Thread
 
Similar Threads
Marcus Green exam 2
Marcus Mock exam 2 Threads
Marcus Green mock exam - 2 #22
Marcus Green Exam 2, Question #22
Marcus Green Exam 2 Question 22 On threads