aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes tthread doubt 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 "tthread doubt" Watch "tthread doubt" New topic
Author

tthread doubt

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


This example is from Whizlabs. At line 1 which thread gets yield. main or a?


SCJP-1.5<br />SCWCD-1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Thread's yield() method "causes the currently executing thread object to temporarily pause..."

So which thread is executing when Thread.yield() is called?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
I think its main thread.
So answer should be main thread
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Since Thread.yield() is called inside main()
and works on currently executing thread.so main thread itself would run and would yield().Another thread spawned works in parallel but code inside main is executed by main thread.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
marc, I think currently running thread is a. I think. Correct me if I am wrong.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Because yield() and sleep() are static in Thread, you cant tell another thread to yield or sleep instead they affect the current executing thread.
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
dolly shah.

normally we cant exactly tell which thread is currently running.

but here we have to consider the thread priority. it is 5 for main.
and a has 1 and b has 2.

so when a.start is called it just goes to the runnable state and wait for main to complete as it has more priority. now yield is called. so main is the thread that has to yield.

But since main still has higher prioity than a it will not yield.(as yield is only for equal or same priority)

correct me if I am wrong...!!

Thanks.


SCJP 5.0<br />SCJD working on it
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
Hi

It has nothing to do with priority of Threads.

I'll put it again what marc has said:

Thread's yield() method "causes the currently executing thread object to temporarily pause..."

So just concentrate on which one is the currently executing thread in this piece of code (not in JVM).

Regards
Yogesh
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
thanks, got it now...
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
In this piece of code currently executing thread is a (after a.start()). I think.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by dolly shah:
In this piece of code currently executing thread is a (after a.start()). I think.

Thread 'a' might run after a.start() is called, but it's not running when Thread.yield() is called. Ask yourself: Is this line of code called by the run() method of thread 'a'? If not, how could it execute in thread 'a'?

(And as Yogesh pointed out, this has nothing to do with thread priorities.)
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
I thought the answer to this is main.

i understood from the above post the currently running thread will yield.
but yield is called in main thread only. so when the main thread runs and hits the call to yield, then it will yield to any equal priority threads if there are any.

a and b thread do not have a yield in its run method or anymethod that run method calls.
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
Please try this

This will clear your doubts.

public class MyThread1 extends Thread {

public void run(){ m1(); }

MyThread1(String threadName) {
super(threadName); }

public synchronized void m1(){
System.out.println("Inside m1() Thread"+Thread.currentThread().getName());
}

public static void main(String[] args){
MyThread1 a=new MyThread1("A");
MyThread1 b=new MyThread1("B");
a.setPriority(Thread.MIN_PRIORITY);
b.setPriority(Thread.MAX_PRIORITY);
System.out.println("1: Thread:"+Thread.currentThread().getName());
a.start();
System.out.println("2: Thread"+Thread.currentThread().getName());
Thread.yield();//line 1
System.out.println("3: Thread"+Thread.currentThread().getName());
b.start();
System.out.println("4: Thread"+Thread.currentThread().getName());

}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by mary john:
...it will yield to any equal priority threads if there are any...

When a thread yields, the platform-dependent thread scheduler determines what other threads might run, and priorities are not a good bet.

As Horstmann and Cornell warn on page 19 of Core Java 2: Volume II -- Advanced Features...
...thread priorities are highly system dependent...

For example, Windows NT/XP has seven priority levels. Some of the Java priorities will map to the same operating system level. In the Sun JVM for Linux, thread priorities are ignored altogether -- all threads have the same priority.

Thus, it is best to treat thread priorities only as hints to the scheduler...
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
"In the Sun JVM for Linux, thread priorities are ignored altogether -- all threads have the same priority." I can attest to the fact that priorities are ignored on Linux JDK. I'm using Kubuntu 7x (kernal 2.6.xx) with 1.6jdk and I've tried the MIN_PRIORITY vs MAX_PRIORITY and also numbers between 1-10, etc., and see no change in my output. Threading seems to be dolled out very 'fairly' in general. I wish I read this thread first! I was starting to wonder what the heck was going on LOL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tthread doubt
 
Similar Threads
Method Synchronized
Confuon in Thread Problem
Thread4
Reg. Thread basics question
Reg. Some Thread basics