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
Code java:
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);
}
}
Code java:
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();
}
}
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.
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?
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
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;
Code java:
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
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.
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?
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.
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...
Code java:
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);
}
}
Code java:
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?
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.
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;
Code java:
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.
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.
Re: Thread synchronisation help!
So I edited run() to help me try and trace what the program is doing at each step;
Code java:
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)
Re: Thread synchronisation help!
Quote:
why there are bits missing
Can you explain what is missing?
Quote:
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.
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!