File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes I want to demonstrate an unsafe thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "I want to demonstrate an unsafe thread" Watch "I want to demonstrate an unsafe thread" New topic
Author

I want to demonstrate an unsafe thread

chinedu iroanyah
Greenhorn

Joined: Mar 12, 2010
Posts: 12
please could some one help me check my code out. I am trying to show how a thread can be unsafe if not synchronized to make it print the “Unsafe operation detected” message by increasing the counter in the while loop (in the run() method) until that happens since I've been trying that but it keeps printing the "Fortuitously executed successfully" message meaning the thread is safe please help...below is my code:

class Counter implements Runnable
{
private int counter;
boolean flag = false;
public int get()
{
return counter;
}
public void set(int n)
{
counter = n;
}
public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}
public boolean getFlag()
{
return flag;
}
public void setFlag()
{
flag = true;
}
public void run()
{
set(0);
while( get() < 10 )
{
//counter++;
increment();
}
if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
}
public static void main(String[] args)
{
Runnable myRun = new Counter();
Thread t1 = new Thread(myRun);
Thread t2 = new Thread(myRun);
t1.start();
t2.start();
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40



It would really help if you posted code that were more readable -- I tried to add code tags to help, but it looks like your code doesn't have any formatting.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
chinedu iroanyah
Greenhorn

Joined: Mar 12, 2010
Posts: 12
sorry for that, here is a better formatted code:


class Counter implements Runnable
{
private int counter;
boolean flag = false;

public int get()
{
return counter;
}//end of get() method


public void set(int n)
{
counter = n;
}//end of set() method

public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}//end of increment() method


public boolean getFlag()
{
return flag;
}//end of getFlag() method


public void setFlag()
{
flag = true;
}//end of setFlag() method


public void run()
{
set(0);
while( get() < 10 )
{
//counter++;
increment();
}//end of while loop

if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
}//end of method run()


public static void main(String[] args)
{
Runnable myRun = new Counter();
Thread t1 = new Thread(myRun);
Thread t2 = new Thread(myRun);
t1.start();
t2.start();
}//end of method main
}//end of class Counter
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

chinedu iroanyah wrote:sorry for that, here is a better formatted code:




Two suggestions.

1. Do more iterations. 10 iterations is way too small. It is possible for a thread to start, and do the 10 iterations before the other thread even starts. Try a bigger number -- like 10 million iterations.

2. Have each thread do separate counts. Instead of having both threads work until the counter hits 10 million, have each thread do exactly 5 million iterations.... then after both threads have completed, check to see if the total is correct. This way, you can tell if the threads accidentally redid iterations.

Henry

chinedu iroanyah
Greenhorn

Joined: Mar 12, 2010
Posts: 12
thank you so much for the help so far, but I have tried increasing the loop to 10 million iterations but it still does not show the "Unsafe operation detected" output. please could you to see if am wrong. here is the code:


Author Message
Henry Wong
Post Today 8:46:35 AM Subject: I want to demonstrate an unsafe thread

chinedu iroanyah wrote:sorry for that, here is a better formatted code:


view plaincopy to clipboardprint?

class Counter implements Runnable
{
private int counter;
boolean flag = false;

public int get()
{
return counter;
}

public void set(int n)
{
counter = n;
}

public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}

public boolean getFlag()
{
return flag;
}

public void setFlag()
{
flag = true;
}

public void run()
{
set(0);
while( get() < 10 )
{
//counter++;
increment();
}
if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
}

public static void main(String[] args)
{
Runnable myRun = new Counter();
Thread t1 = new Thread(myRun);
Thread t2 = new Thread(myRun);
t1.start();
t2.start();
}
}



Two suggestions.

1. Do more iterations. 10 iterations is way too small. It is possible for a thread to start, and do the 10 iterations before the other thread even starts. Try a bigger number -- like 10 million iterations.

2. Have each thread do separate counts. Instead of having both threads work until the counter hits 10 million, have each thread do exactly 5 million iterations.... then after both threads have completed, check to see if the total is correct. This way, you can tell if the threads accidentally redid iterations.

Henry

chinedu iroanyah
Post Today 8:39:01 AM Subject: I want to demonstrate an unsafe thread
sorry for that, here is a better formatted code:


class Counter implements Runnable
{
private int counter;
boolean flag = false;

public int get()
{
return counter;
}//end of get() method


public void set(int n)
{
counter = n;
}//end of set() method

public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}//end of increment() method


public boolean getFlag()
{
return flag;
}//end of getFlag() method


public void setFlag()
{
flag = true;
}//end of setFlag() method


public void run()
{
set(0);
while( get() < 10 )
{
//counter++;
increment();
}//end of while loop

if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
}//end of method run()


public static void main(String[] args)
{
Runnable myRun = new Counter();
Thread t1 = new Thread(myRun);
Thread t2 = new Thread(myRun);
t1.start();
t2.start();
}//end of method main
}//end of class Counter
Henry Wong
Post Today 8:23:09 AM Subject: I want to demonstrate an unsafe thread


It would really help if you posted code that were more readable -- I tried to add code tags to help, but it looks like your code doesn't have any formatting.

Henry
chinedu iroanyah
Post Today 8:04:05 AM Subject: I want to demonstrate an unsafe thread
please could some one help me check my code out. I am trying to show how a thread can be unsafe if not synchronized to make it print the “Unsafe operation detected” message by increasing the counter in the while loop (in the run() method) until that happens since I've been trying that but it keeps printing the "Fortuitously executed successfully" message meaning the thread is safe please help...below is my code:

class Counter implements Runnable
{
private int counter;
boolean flag = false;
public int get()
{
return counter;
}
public void set(int n)
{
counter = n;
}
public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}
public boolean getFlag()
{
return flag;
}
public void setFlag()
{
flag = true;
}
public void run()
{
set(0);
//I increased the iteration to 10000000
while( get() < 10000 )
{
//counter++;
increment();
}
if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
}
public static void main(String[] args)
{
Runnable myRun = new Counter();
Thread t1 = new Thread(myRun);
Thread t2 = new Thread(myRun);
t1.start();
t2.start();
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Henry Wong wrote:
Two suggestions.

1. Do more iterations. 10 iterations is way too small. It is possible for a thread to start, and do the 10 iterations before the other thread even starts. Try a bigger number -- like 10 million iterations.

2. Have each thread do separate counts. Instead of having both threads work until the counter hits 10 million, have each thread do exactly 5 million iterations.... then after both threads have completed, check to see if the total is correct. This way, you can tell if the threads accidentally redid iterations.


chinedu iroanyah wrote:thank you so much for the help so far, but I have tried increasing the loop to 10 million iterations but it still does not show the "Unsafe operation detected" output. please could you to see if am wrong. here is the code:


I gave you two suggestion because I noticed two issues that could be preventing you from detecting the race condition. You solved one -- now try to do the other.

BTW, you really need to fix your posts. Now, it is difficult to read, and a mess too.

Henry
chinedu iroanyah
Greenhorn

Joined: Mar 12, 2010
Posts: 12
once again thank you so much for all the help, this time i used the second option that you gave me but still it does not print the "Unsafe operation detected". sorry for the formatting am not too familiar with posting


class Counter implements Runnable
{
private int counter,iteration;
boolean flag = false;


public Counter(int count)
{
iteration = count;
}



public int get()
{
return counter;
}//end method get


public int getIteration()
{
return iteration;
}//end consrtuctor



public void set(int n)
{
counter = n;
}//end method set


public void increment()
{
int temp1 = counter;
counter++;
int temp2 = counter;
if( temp1 == temp2 || (temp2-temp1) > 1 )
setFlag();

}//end method increment


public boolean getFlag()
{
return flag;
}//end method getFlag


public void setFlag()
{
flag = true;
}//end method setFlag


public void run()
{
set(0);
while( get() < iteration )
{
//counter++;
increment();
}//end while

if (getFlag() == true )
System.out.println( "Unsafe operation detected" );
else
System.out.println( "Fortuitously executed successfully");
System.out.println( this.getIteration());
} //end method run()



public static void main(String[] args)
{
//Runnable myRun = new Counter();
Thread t1 = new Thread(new Counter(5000000));
Thread t2 = new Thread(new Counter(5000000));
t1.start();
t2.start();
} //end method main
} //end class Counter
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

See how much easier Henry's code is than yours? When posting code, please UseCodeTags.(←click)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

chinedu iroanyah wrote:once again thank you so much for all the help, this time i used the second option that you gave me but still it does not print the "Unsafe operation detected". sorry for the formatting am not too familiar with posting



Your new code now has two counters -- there isn't any sharing of data between the two threads.

Henry
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time repeating each others' answers.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Jeff Verdegan wrote:Please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time repeating each others' answers.


Agreed. And FYI...

https://forums.oracle.com/forums/thread.jspa?threadID=2361651&tstart=30

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

BTW, just converted your test application -- with the two changes that I suggested. And cleaned it up a bit too.

Henry


chinedu iroanyah
Greenhorn

Joined: Mar 12, 2010
Posts: 12
thank you so much Henry, I really am grateful for the help. you have totally solved it!!! thank you once again
 
 
subject: I want to demonstrate an unsafe thread