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 15 of 15

Thread: Thread synchronisation help!

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Thread synchronisation help!

    Hi whenever I try to run Numbers.java I get;

    NumberPrinter.java:8: error: cannot find symbol
    setPriority(n);
    ^

    why???????????????????????
    The textbook I'm studying from uses this exact code as an example but it just doens't seem to be working for me


    public class NumberPrinter implements Runnable
    {
    	int num;
     
    	public NumberPrinter (int n)
    	{
    		num = n;
    		setPriority(n);
    	}
     
    	public void run()
    	{
    		for (int k=0; k<10; k++)
    			System.out.print(num);
     
    	}
    }


    public class Numbers
    {
    	public static void main (String args[])
    	{
    		Thread number1, number2, number3, number4, number5;
     
    		number1 = new Thread(new NumberPrinter(1)); number1.start();
    		number2 = new Thread(new NumberPrinter(2)); number2.start();
    		number3 = new Thread(new NumberPrinter(3)); number3.start();
    		number4 = new Thread(new NumberPrinter(4)); number4.start();
    		number5 = new Thread(new NumberPrinter(5)); number5.start();
    	}
    }


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

    Default Re: Thread synchronisation help!

    Where is the setPriority() method defined? It looks like the compiler can not find its definition in the class where it is used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 22nd, 2012)

  4. #3
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    Well the book seems so be suggesting that setPriority() is part of the interface Runnable, or at least that I can use it in this situation by simply calling it. Am I misunderstanding this entirely?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    An interface does not define methods. When a class implements an interface, the class is required to define(provide code for) all the methods defined in the interface.
    http://docs.oracle.com/javase/tutori...interface.html
    http://docs.oracle.com/javase/tutori...interface.html

    Did you read the API doc for the Runnable interface? Was there any mention of setPriority()?
    What classes is the setPriority() method defined in? Again see the API doc.

    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 22nd, 2012)

  7. #5
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    Thanks!

    I looked it up and it appears that the setPriority method is defined in the Thread class.
    I tried calling it using Thread.sePriority() but got an error for trying to call a non-static method from a static context.

    So I instead instantiated a new copy of Thread in the NumberPrinter class thusly;

    public class NumberPrinter implements Runnable
    {
    	int num;
    	Thread t = new Thread ();
     
    	public NumberPrinter (int n)
    	{
    		num = n;
    		t.setPriority(n);
    	}
     
    	public void run()
    	{
    		for (int k=0; k<2000000; k++)
    			if  (k%1000000 == 0)
    				System.out.print(num);
     
    	}
    }

    The program now compiles and runs, but I still get interferece!

    The code now outputs the following;

    1321243455

    But according to the example in the book, I should be getting 5544332211

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    How does creating a thread and setting its priority change the priority of the thread that is executing the method? If you want to change the priority of the thread for the executing method, you need to get a reference to the thread that the method is using. Look at the Thread class for a method to get a reference to the thread that the executing method is using.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 22nd, 2012)

  10. #7
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    Do you mean the currentThread method? I don't understand how I am meant to be using it...

    Should I some how be referencing the run () method from NumberPrinter.java in the main() method of Numbers.java?

    To your knowledge, do java textbooks tend to have errors in them like this one? I have copied their code to the letter yet it yields different results, what could the reason for that be?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    That may be the right method to use. You need to get a reference to the thread that is executing so you can change its priority.

    Using Threads this way is tricky. I'm not sure you will get the same results as the textbook.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 22nd, 2012)

  13. #9
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    so it turns out I was looking at it wrong, the code is slightly different (this book is really confusing), but the revised version still doesn't work.
    The textbook claims it will print out 5544332211 but it is rather being printed out in a seemingly random order...

    public class NumberThread extends Thread
    {
    	int num;
     
    	public NumberThread (int n)
    	{
    		num = n;
    		setPriority (n);
    	}
     
    	public void run()
    	{
    		for (int k=0; k<2000000; k++)
    			if (k%1000000 == 0)
    				System.out.print(num);
    	}
    }

    public class Numbers
    {
    	public static void main (String args[])
    	{
    		Thread number1, number2, number3, number4, number5;
     
    		number1 = new NumberThread(1); number1.start();
    		number2 = new NumberThread(2); number2.start();
    		number3 = new NumberThread(3); number3.start();
    		number4 = new NumberThread(4); number4.start();
    		number5 = new NumberThread(5); number5.start();
    	}
    }

    I'm guessing I still have the same problem, as I am still not really referencing a specific method in Numbers.java. But doesn't the fact that I am calling the constructor from NumberThread (which includes setPriority()) tell the program to to set the priority for each thread which is executed?

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    When the class extends Thread, setPriority() is in the Thread class and the code should now compile.

    Try doing some debugging by adding some println methods to show when the methods are executed and when the methods are exited by printing out a message at the start and at the end of the method. Seeing the order of the messages will help you understand how the threads are executing.

    The posted code will set the priority of the thread when setPriority() is executed.
    How much have any of the other threads executed by then?
    If you start thread 1 before any other threads, it can execute and be done before the others are started.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 22nd, 2012)

  16. #11
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    Thanks for the advice norm, but this actually looks hopeless. The numbers are printed out in random order regardless of how they are ordered in the main method or what priority I set them, if I am even doing that correctly. if I change run() to this;

    	public void run()
    	{
    		for (int k=0; k<20; k++)
    			//if (k%1000000 == 0)
    				System.out.println(num);
     
    		System.out.println ("OK");
    	}

    ...I get a few random numbers at the start, then a long list of 1s, 2s, 3s, 4s then 5s. Not what I wanted at all! Thanks for your patience but I am probably going to just give up because I can't handle this book lying to me.

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    The println does not say what object it is in (each has a unique num value)
    and does not print when the method begins - add another println at the beginning of the method so you see when the method starts execution.
    Why is the if statement commented out? It is needed to reduce the print outs.

    This problem will take many changes for you to see what is happening. It is NOT as simple as the book says. Make the above changes and see what is printed.
    Then look at when the print() method inside the if is called. When will the if be true? If you can't see, print out the value of k in addition to num.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #13
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    So I edited run() to help me try and trace what the program is doing at each step;

    	public void run()
    	{
    		System.out.println("Begin of run("+num+")");
    		for (int k=0; k<2000000; k++)
    			if (k%1000000 == 0)
    				System.out.println(num);
    		System.out.println("End of run("+num+")");
    		//System.out.println("k="+k);
    	}

    and the output, to me, makes no sense....I don't get why there are bits missing, and the order of numbers just seems random.

    For general synchronization purposes, should I just forget about the setPriority method and maybe just stick with 'synchronize'?

    The Output:

    Begin of run(1)
    1
    Begin of run(3)
    Begin of run(4)
    4
    Begin of run(5)
    5
    3
    5
    4
    End of run(5)
    End of run(4)
    3
    Begin of run(2)
    2
    End of run(3)
    1
    2
    End of run(1)
    End of run(2)

  19. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread synchronisation help!

    why there are bits missing
    Can you explain what is missing?

    the order of numbers just seems random.
    The order of the output shows the order the statements were executed in. Which thread gets execution will depend on which one is ready to execute and also on what else is executing on your PC.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 24th, 2012)

  21. #15
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Thread synchronisation help!

    ah there is nothing missing I made a mistake there. I am finding this way too difficult to grasp so I am going to focus on using synchronisation with monitors instead. Thanks for the help!

Similar Threads

  1. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  2. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  3. My First thread
    By shyandeep chatterjee in forum Member Introductions
    Replies: 0
    Last Post: April 18th, 2012, 05:06 AM
  4. Help with Synchronisation with a semaphore
    By Stockholm Syndrome in forum Threads
    Replies: 5
    Last Post: March 8th, 2012, 09:36 PM
  5. What is this thread for exactly?
    By javapenguin in forum Computer Support
    Replies: 2
    Last Post: July 2nd, 2011, 10:37 AM