• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

thread.yield();

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to thread.yield(). the current working thread is stopped and the other thread is started which is ready to run state.( Please tell me whether this is right or wrong..)
now this program.....


class worker implements Runnable
{
private Thread theThread;
public void kickStart()
{
if(theThread==null)
{
theThread=new Thread(this);
theThread.start();
}
}

public void terminate()
{
theThread=null;
}

public void run()
{
while(theThread==Thread.currentThread())
{
System.out.println("going ard in loops");
}
}
}


public class controller
{
public static void main(String args[])
{
worker w=new worker();
w.kickStart();
Thread.yield();
w.terminate();
}
}
the output varies every time we run this program can u tell how this happens..........
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the code, the number of output line about "going ard in loops" will differ from time to time. I think it is because the yield time duration for the current thread will be different for each time.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because yeild() method pauses execution of the current thread and allows other threads to execute. We don't know how long it pauses the execution of the current thread. So, the amount of time the other thread, which prints out "going ard in loops", gets by JVM may be different for each execution of the program and prints different number of lines of output.
 
Srinivas Bitla
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have come up with a little different version of this program to understand how synchronized keyword works. I have written this program such that the program exits when the values of i and j are out of sync in SyncData object. If the program doesn't exit then it meens that the values of i and j are in sync.

Though the sync() method is synchronized, the program exits after executing for some time. I don't understand why those values are going out of sync though the sync() is synchronized. Does anyone have any idea?

Following is the program. Just compile and run it.

Thanks,
Srinivas.




package com.test;

public class Controller {

public static void main(String args[]) {

// JVM will have only one instance of SyncData, i.e data
SyncData data = SyncData.getInstance();
Worker w1 = new Worker(data, true);
w1.kickStart();
Thread.yield();
Worker w2 = new Worker(data, false);
w2.kickStart();
Thread.yield();
while(true) {
if(data.inSync()) {
System.out.println("In Sync, i = " + data.getI() + " and j = " + data.getJ() + " continuing...");
}
else {
System.out.println("Not in Sync, i = " + data.getI() + " and j = " + data.getJ());
w1.terminate();
w2.terminate();
break;
}
}
}
}

class Worker implements Runnable {

private SyncData data;
private boolean forward;
private Thread theThread;

public Worker(SyncData data, boolean forward) {
this.data = data;
this.forward = forward;
}

public void kickStart() {

if(theThread==null) {
theThread=new Thread(this);
theThread.start();
}
}

public void terminate() {

theThread=null;
}

public void run() {

double d = 0.0D;
while(theThread==Thread.currentThread()) {

if(forward) {
d = d + 1;
}
else {
d = d - 1;
}
this.data.sync(d);
}
}
}

//Its a singleton, to make sure JVM has only one instance of it.
class SyncData {

private static SyncData data;
private SyncData() {
}

public static SyncData getInstance() {
if(data == null) {
data = new SyncData();
}
return data;
}

private double i = 0;
private double j = 0;

// Makes sure that i and j are always in sync.
// This is synchronized to make sure i and j are always in sync.
// But doesn't seem to work.
// What am I missing?
public void sync(double syncValue) {
synchronized (this) {
System.out.println("Sync called with " + syncValue);
this.i = syncValue;
// Kill some time. Don't yeild.
for(int x=0; x<1000; x++) {
new Object();
}
this.j = syncValue;
}
}

public boolean inSync() {
return i==j;
}

public double getI() {
return i;
}

public double getJ() {
return j;
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivas Bitla, I think you need to synchronize the inSync() method.
 
sandeep dhingra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means thread.yield() method pauses the execution of the current thread for some period of ttime and that is not fixed it can very everytime the program is run by the jvm.
and then the ccontrol comes back to the original thread....
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic