Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with synchronizations  RSS feed

 
Kash Mhai
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys!
I am trying to create two threads that are gonna be accessing a method execute(). This method just prints values from 0 to 499. My problem is that the threads should execute one after the other... how do i do that?

Thanks in advance
------------------

/*
* ThreadExp.java
*
* Created on April 1, 2005, 1:10 PM
*/

/**
*
* @author 1083996
*/

import java.util.HashMap;
class SMSThread implements Runnable
{
private Thread smsThread = null;
private String threadName = null;
private static boolean bFree = true;
private static HashMap hsBox = null;

public SMSThread(String strName)
{
threadName = strName;
if (hsBox==null)
hsBox = new HashMap();
}

public void start()
{
if (smsThread==null)
{
smsThread = new Thread(this,threadName);
hsBox.put(threadName, smsThread);
smsThread.start();
}
}

public void run()
{
try
{
System.out.println(smsThread.getName()+" is Running");
synchronized(this)
{
while (bFree != true)
{
System.out.println(smsThread.getName()+" is in Waiting");
wait();
}
if (execute())
{
bFree = true;
System.out.println(smsThread.getName()+"----------------Notified All: "+bFree);
notifyAll();
}
System.out.println(smsThread.getName()+"----------------Notified All: "+bFree);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

public boolean execute()
{
bFree = false;
System.out.println("-------------------->Locked by "+smsThread.getName());
for (int i=0;i<500;i++)
{
System.out.println("Thread-->: "+smsThread.getName()+": "+i);
}
System.out.println("-------------------->UnLocked by "+smsThread.getName());
return true;
}
}
public class ThreadExp {

/** Creates a new instance of ThreadExp */
public ThreadExp() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new SMSThread("Thread1").start();
new SMSThread("Thread2").start();
}

}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

If you just make a method:

then only one thread can enter the method at one time. Whichever thread gets there first will print 0..499 while the other thread is blocked. Then the second thread will print 0..499. The calls from the other threads don't need anything special about them at all. Your main might look like:

and your Caller like

I left a bunch out, but does that make enough sense to give it a try?
 
Saurav Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two threads execute and acquire two separate locks (there are two distinct objects) using 'this'.The notifyAll of the first thread has no effect
on the wait of the second thread. Replace
<code>
while (bFree != true)
{
System.out.println(smsThread.getName()+" is in Waiting");
wait();
}
</code>
with
<code>
while (bFree != true)
{
System.out.println(smsThread.getName()+" is in Waiting");
try {
wait(300);
}
catch (InterruptedException e) {}
}
}
</code>
 
Kash Mhai
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
It worked.
The mistake i did was that the method which needs synchronization should have been in a seperate class. Since that was not the scenario, the two threads were aquiring seperate locks...!!

Thanks again guys!

Kash
"A mistake learnt is wisdon gained.."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!