Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Threads giving "current thread not owner" exception

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Threads giving "current thread not owner" exception

    could someone help me regarding this following code. It's large but in essence, i'm just running 4 threads
    1. to count
    2. to check if count is even
    3. to check if count is odd
    4. to check if count is prime (this one is supposed to run only if 2 & 3 are done with thread 1 for one iteration).

    Errors: (it prints the following sequence, and then gets stuck forever at bb.wait)

    count
    Reaching A
    aa.wait
    Reaching B
    Reaching C
    outside a in C
    ss in A wait
    bb.wait

    /*	Write a thread program that can count from 1 till 100 with the following conditions :-
    1. Have three threads running simultaneously: Thread A ,Thread B,Thread C. Thread A will count only the even numbers, Thread B will count only the odd numbers, Thread C will count only prime numbers.
    2. When ever the multiples of 10 is reached for Thread A , let it sleep for 2 secs, When ever the multiples of 9 is reached for Thread B , let it sleep for 2 secs. Thread C count should always be less than A or B. Only after thread A / thread B have counted a number, may C check whether it's prime number or not.
    */
     
    class store
    {
    	int i;
     
    	synchronized void set(int k)
    	{
    		i = k;
    	}
     
    	synchronized int get()
    	{
    		return i;
    	}
     
    }
     
    class counts extends Thread
    {
    	int i; store ss;
     
    	counts(store S)
    	{
    		ss = S;
    	}
     
    	public void run()
    	{
    		synchronized (ss)
    		{
    			ss.notify();
    			while( i < 100)
    			{
    				i = ss.get();
    				i++;
    				ss.set(i);
    				try 
    				{
    					System.out.println("count");
    					ss.wait(); 
    				} catch (InterruptedException e){}
     
    			}
    		}
    		System.exit(1);
     
    	}
    }
     
    class A extends Thread
    {
    	int i = 0; boolean Aboo = false; 
    	store ss; A aa;
     
    	A(){}
     
    	A(store S)
    	{
    		ss = S;
    	}
     
    	public void run()
    	{
    		System.out.println("Reaching A");
    		synchronized (ss)
    		{
    			synchronized (this)
    			{
    				notify();
    				while (i< 100)
    				{	
    					ss.notifyAll(); 
    					i = ss.get();
     
    					if (i%2 == 0)
    					{
    						System.out.println("Counted Even in A \t" + i);
    						Aboo = true;
    						if (i%10 == 0)
    						{
    							try
    							{
    								this.sleep(2000);
    							}
    							catch(InterruptedException e) 
    							{	
    								System.out.println("A InterruptedException caught"); 
    							} 
    						} 
    					} 
     
    					else Aboo = false;
     
    					try
    					{System.out.println("aa.wait");
    						wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class B error \n");
    					}
     
    					try
    					{System.out.println("ss in A wait");
    						ss.wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class A error");
    					}
     
    				}
    			}
    		}
    		System.exit(1);
    	}
     
    	boolean bool()
    	{
    		return Aboo;
    	}
     
    	int Ai()
    	{
    		return i;
    	}
     
    }
     
    class B extends Thread
    {
    	int i = 0; boolean Bboo = false; 
    	store ss; B bb;
     
    	B(store S)
    	{
    		ss = S;
    	}
     
    	public void run()
    	{System.out.println("Reaching B");
    		synchronized (ss)
    		{
    			synchronized (this)
    			{
    				notify();
    				while (i< 100)
    				{	
    					ss.notifyAll(); 
    					i = ss.get();
     
    					if (i%2 == 0)
    					{
    						System.out.println("Counted Even in B \t" + i);
    						Bboo = true;
     
    						if (i%9 == 0)
    						{
    							try
    							{
    								this.sleep(2000);
    							}
    							catch(InterruptedException e) 
    							{	
    								System.out.println("B InterruptedException caught"); 
    							} 
    						}
    					}
     
    					else Bboo = false;
     
    					try
    					{System.out.println("bb.wait");
    						wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class B error \n");
    					}
     
    					try
    					{System.out.println("ss in B wait");
    						ss.wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class B error");
    					}
     
    				}
    			}
    		}
    		System.exit(1);
    	}
     
    	boolean bool()
    	{
    		return Bboo;
    	}
     
    	int Bi()
    	{
    		return i;
    	}
    }
     
    class C extends Thread
    {	
    	A a; B b; int i = 0;
    	store ss;
     
    	C(A aa, B bb)
    	{
    		a = aa; 
    		b = bb;
    	}
     
    	public void Check(int k)
    	{
    		int j = 1;
     
    		while (j < k)
    		{
    			if (k%j == 0)
    			{
    				break;
    			}
    			j++;
    		}
     
    		if (j == k)
    		{
    			System.out.println("counted a Prime Number\t" +i);
    		}
    	}
     
    	public synchronized void run()
    	{
    		System.out.println("Reaching C");
     
    		synchronized (a)
    		{
    			a.notify();
    			if (a.bool())
    			{
    				i = a.Ai();
    				while (i < 100)
    				{
    					Check(i);
    					try
    					{	System.out.println("a and s in C");
    						a.wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class C error \n");
    					}	
    				}
    			}
    			else try{System.out.println(" outside a in C"); a.wait();} catch(Exception ee){}
    		}
     
    		synchronized (b)
    		{
    			b.notify();
    			if (b.bool())
    			{
    				i = b.Bi();
    				while (i < 100)
    				{
    					Check(i);
    					try
    					{	System.out.println("a and s in C");
    						b.wait();
    					}
    					catch (InterruptedException e)
    					{
    						System.out.println("class C error \n");
    					}	
    				}
    			}
    			else try{System.out.println(" outside b in C"); b.wait();} catch(Exception ee){}
    		}
    		System.exit(1);
    	}
    }
     
    public class OddEvenPrime
    {
    	public static void main(String[] arg)
    	{
    		store ss = new store();
    		ss.set(0);
     
    		counts countin = new counts(ss);
    		Thread thcount = new Thread(countin);
    		thcount.setName("thcount");
     
    		A aa = new A(ss);
    		Thread Aeven = new Thread(aa);
    		Aeven.setName("Aeven");
     
    		B bb = new B(ss);
    		Thread Bodd  = new Thread(bb);
    		Bodd.setName("Bodd");
     
    		C cc = new C(aa, bb);
    		Thread Cprime  = new Thread(cc);
    		Cprime.setName("Cprime");
     
    		thcount.start();
    		Aeven.start();
    		Bodd.start();
    		Cprime.start();
     
    	}
    }
    Last edited by shashank90; August 8th, 2011 at 12:40 PM. Reason: Added highlight tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Threads giving "current thread not owner" exception

    Please edit your code and wrap it with code tags. See: BB Code List - Java Programming Forums
    Or use the Go Advanced button and use the #icon

    Please post the full text of the error message here.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Threads giving "current thread not owner" exception

    I hope i'm more specific now... it's just a synchronization issue, but I can't get it right. using sleep() is easy, I wanted it to be as fast as possible.
    Any suggestions what could be wrong... ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Threads giving "current thread not owner" exception

    There is a lot of interrelated logic with wait and notify and synchronized in your code.
    Why is it so complicated? Why so many different threads? Do you have a processor where all these threads can run concurrently?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Threads giving "current thread not owner" exception

    The code has NO comments. There is no easy way to wade through all that code and figure out what you are trying to do.

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Exception in thread "main" java.lang.NoClassDefFoundError
    By Scarice in forum Java Theory & Questions
    Replies: 25
    Last Post: July 4th, 2011, 10:02 PM
  3. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM

Tags for this Thread