• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

tthread doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This example is from Whizlabs. At line 1 which thread gets yield. main or a?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread's yield() method "causes the currently executing thread object to temporarily pause..."

So which thread is executing when Thread.yield() is called?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its main thread.
So answer should be main thread
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc, I think currently running thread is a. I think. Correct me if I am wrong.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because yield() and sleep() are static in Thread, you cant tell another thread to yield or sleep instead they affect the current executing thread.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yogesh Baraskar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, got it now...
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this piece of code currently executing thread is a (after a.start()). I think.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic