Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I don't know what's exactly causing this. I think there shouldn't be any race condition, yet my global variable is always inconsistent no matter how many time I run my program.

Java
public class main{


	public static int global;




	public static void main(String[] args){

			Girls [] girls = new Girls[10];


			Guys [] guys = new Guys[10];

			for(int i = 0; i < 10; i++)
			{


				girls[i] = new Girls();
				guys[i] = new Guys();

			}


			for(int i = 0; i < 10; i++)
			{

				girls[i].start();

				guys[i].start();



			}




			for(int i = 0; i < 10; i++)
			{
				try{



					girls[i].join();
					guys[i].join();



				} catch(InterruptedException e){
					System.out.println("exc caught");
				}


			}


	}
}


Java
public class Guys extends Thread{



		public static void go()throws InterruptedException{

			final Lock lock = new ReentrantLock();



			lock.lock();


			System.out.println("global " + main.global);



			main.global = main.global + 100;

			lock.unlock();


		}






	public void run(){

		try{

		FileControl2 fic = new FileControl2();


			for(int i = 0; i <1; i++)
			{
				System.out.println(this);


				fic.GuysEntry();

				go();

				fic.GuysExit();

			}
		} catch(InterruptedException e)
		{
			System.out.println("Interrupted Exception caught");
		}

	}


}


Java
public class FileControl2 {

	final Lock lock = new ReentrantLock();
	final Condition guys = lock.newCondition();
	final Condition girls = lock.newCondition();
	int nGirls = 0;
	int nGuys = 0;



	public void GuysEntry()throws InterruptedException{
		lock.lock();
		try{
			if(nGirls > 0)
			{
				guys.await();
			}
			nGuys++;




		   }
		finally{
			lock.unlock();
		}


	}



	public void GuysExit()throws InterruptedException{
		lock.lock();
		try{
			girls.signal();
			nGuys--;

		}
		finally{

			lock.unlock();
		}

	}


	public void GirlsEntry()throws InterruptedException{
		lock.lock();
		try{

			if(nGuys > 0)
			{
				girls.await();
			}
			nGirls++;


		}
		finally{

			lock.unlock();
		}

	}

	public void GirlsExit()throws InterruptedException{
		lock.lock();
		try{
			guys.signal();
			nGirls--;

		}
		finally{
			lock.unlock();
		}

	}


}


The output I expect is global 100 global 200 global 300 global 400 global 500 global 600 global 700 global 800 global 900 global 1000

but I get something like global 100 global 100 global 100 global 200 global 200 global 600 global 200
Posted
Updated 15-Oct-13 19:01pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 2:47am    
Please, language, platform..? You always need to tag those things.
For using shared objects, you need to use thread synchronization primitives, such as lock. Or mutex.
—SA

 
Share this answer
 
In .NET net, there is no such thing as "global variable". It does not change anything. The important aspect is not "global", but the fact you share any objects between threads.

As I already explained, sharing of objects should be minimized. You should try to do the most calculations on stack, as, of course, each thread means using a separate stack. The best synchronization is no synchronization.

Now, with shared objects, this is the simple pattern which works without race conditions and deadlocks: all access to shared object is guarded by the lock, same shared object with the same lock, locks should be strictly nested (one simplest violation of nesting is at the same time one of the simplest "textbook" example of a deadlock). It is important to release the lock and preserve strict nesting in the case of any exception thrown inside the mutual exclusion area.

Please see again, all links a in my recent answer:
Is this enough to insure mutual exclusion?[^].

You need to take it all in more serious, regular and fundamental way then you are trying to do with your questions. You need to learn the whole topic on good level and educate yourself on a regular basis.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900