Thread in infinite loop, won't allow anything else to happen...help appreci
Basically, i'm trying to make a basic traffic simulator, and learn java at the same time, so bare with me...
I want a couple of threads to run concurrently in the applet, but am having touble. Basically one starts inside the run() and works fine, but the other wont...infact nothing after the first thread will run (i.e. if the repaint() goes after the end of catch{}). The source is a bit of a mess with no comments, but its all really basic so should be easy for even the elementary java programmer to understand.
I know the second thread (well what i hope is a second thread) doesn't actually do anything other than output a line that could be done by the first thread, but its just proof of concept for now, i will put stuff in there later.
source, class and html are here:
http://www.soton.ac.uk/~tr403/traff/
also the slider in the bottom right doesn't appear in the browser unless you click on it, but in the applet viewer it comes up...why is this? its at the bottom of my priorities at the mo, but its puzzling, and obviously something really elementary.
Any help MUCH appreciated, its for my dissertation which has got be to handed in on may 11th ;)
This the run bit, initated from the start [method? not sure on terminology yet]
publicvoid run()
{
Thread carmove = Thread.currentThread();
try{
while (carmove !=null){
Thread.currentThread().sleep(carsptimeout);
xcount++;
repaint(0,144,300,5);
if (xcount == maxlength1){
xcount = 0;
}
}
}
catch (InterruptedException e){}
Thread sysline = Thread.currentThread();
try{
while (sysline !=null){
Thread.currentThread().sleep(carsptimeout);
System.out.println(xcount);
}
}
catch (InterruptedException e){}
}
publicvoid stateChanged(ChangeEvent e)
{
tempreadout.setText
(" " + (slider.getValue()));
carsptimeout = 100 - (int)slider.getValue();
}
publicvoid update(Graphics g)
{
paint(g);
}
It's pretty much impossible to debug someone else's code by looking at random snippets of it.But I'll observe that although you mentioned two threads, I only see evidence of one in the code you posted.
right.the full source is here: http://www.soton.ac.uk/~tr403/traff/its 300 somethings lines i didn't want to post the whole lot up.How come there is only one thread, i thought there was two? how can i make the second a separate entity?Thanks.
When you're having a problem it's usually best to create a very small class that exhibits the problem, then debug that (or use it to ask for help on forums). It makes it a lot easier to understand the problem and discuss it, etc. Also people are a lot more likely to help if they don't have to wade through lots of code. Also it's easier to help if the code is legible; you might want to try harder to properly format your code.
Anyway...your code creates two threads, but it has a single run() method which is shared by both. That can be fine when the two threads are supposed to be doing the exact same thing, but it appears that you're trying to make run() service each thread separately. If so that's your big design problem, I think.
In your run() method you have two while loops, both of which test for something that never changes. The latter while loop is only invoked after the first one finishes, but of course it's not going to.
You should probably use inner classes, and have a separate run() method for each thread.
Apologies. I would have tried to break down the part i am having trouble with but its so a mission to get anything to work.
Been reading up on implementing seperate "run"s for each thread...the threads are named carmove and sysline, would something like this work:
public void Runnable carmove(){carmove thread code}
public void Runnable sysline(){sysline thread code)
I know this isn't correct, but is it on the right lines?
Would these two both run concurrently? (even if they both contain permenant while loops), synchronisation between threads is not a problem.
Also while ive got you ;), any idea why the slider doesn't show up on load? Is there a "repaint()" equivilent for swing elements, does the "setVisible(true);" need to be in a certain place (currently in init())?
Thanks for your time.
Thread carmove = Thread.currentThread();
try {
while (carmove != null){
That makes a local var called carmove, and as you do not change that var, it will never be null.
Why is the call called "TemperatureSlider" when it appears to be a traffic lights simulator?
I'd look at creating some more classes (car, traffic light maybe?)
mlka at 2007-7-13 19:37:25 >

> Would these two both run concurrently? (even if they
> both contain permenant while loops), synchronisation
> between threads is not a problem.
Yes.
> Also while ive got you ;), any idea why the slider
> doesn't show up on load? Is there a "repaint()"
You overwrite the update/paint withut calling super.
mlka at 2007-7-13 19:37:25 >

Right, sorted out the stupid ("Temperatureslider") file naming problems, and updated the files on the webspace (http://www.soton.ac.uk/~tr403/traff/). Made a class named "trafficlight".
What is the code to call this classes run() to run as a thread from the run()/start() methods in the main class? (excuse my terminology)class trafficlight extends Tr403traff implements Runnable {
Runnable carmove = new Runnable(){
public void run() {System.out.println(ABC);}
};//close runnable
public void run() {System.out.println(XYZ);
}//close run
}//close class
When it is called, will the system print the var's ABC or XYZ or both?
>You overwrite the update/paint withut calling super.
How do i "call super" and where do i do it? I have seen about this before but didn't really understand the difference between the repaint() and other methods. If its just a simple line just paste it up, if its a complete restructuring job let me know a couple more keywords and i will google it up and work it out myself.
Cheers.
I recomend reading up on Super: http://mindprod.com/jgloss/super.htmlAnd changing the design, so instead of drawing onto the Applet, you create a JPanel to act as the canvas you draw on, and add the canvas to Applet.
mlka at 2007-7-13 19:37:25 >

>I recomend reading up on Super: http://mindprod.com/jgloss/super.html
**** it I read up that page and it didn't make alot of sense, that is; how to apply it to my applet. Will read some more...
>instead of drawing onto the Applet, you create a JPanel to act as the canvas you draw on, and add the canvas to Applet.
This sounds like something i can do...not sure how its gonna effect the slider at the bottom but will do it anyway.
Any ideas on my threads? how can i start two seperate threads to run concurrently?
Cheers, -tim.
whats the starring ("****") about in my message? I didn't put in a swear/offensive word...can't remember when i typed now but was suprised to see that...
> This sounds like something i can do...not sure how
> its gonna effect the slider at the bottom but will do
> it anyway.
Think of your applet as a bunch of cards on top of each other.
+-+
| APPLET|
| |
| |
| +--+ |
| | button | |
| +--+ |
+-+
Each card call the paint on all its childern. You override the paint, so it it does not call "paint" on its childern. By moving the draw into a different JPanel, which has no childern, the fact that you don't ask for its childer to be drawn does not matter.
+-+
| Applet|
| +--+ |
| |DrawTraficlights | |
| +--+ |
| |
| +--+ |
| | button | |
| +--+ |
+-+
> Any ideas on my threads?
Redesign.
Create a class for object type in your world. Only "extend" a different class if that makes sence.
What is a "sysline"?
Why is trafic light moving cars forward?
mlka at 2007-7-13 19:37:25 >

> whats the starring ("****") about in my message? I> didn't put in a swear/offensive word...can't remember> when i typed now but was suprised to see that...[url= http://www.answers.com/****&r=67]D.a.m.n[/url] #5. To swear at.To swear;
mlka at 2007-7-13 19:37:25 >

lol, must have been d.a.m.n...
basically i want two threads...one that moves cars, and one that sits and sleeps and changes the traffic lights every 5 seconds or so.
My dissertation supervisor really likes the idea of having threads so they've got to be in, even if they arn't truely needed...i can visualise how todo this without threads, but they need to be in.
You've kind of lost me a bit with the "extending" of classes, but i think i am with you regarding the painting problems; thanks for taking the time to explain that out...is there a quick fix to it? can i stick repaint() in somewhere and solve the problem?...or maybe even super.repaint()?
The thread was called sysline as it just outputted a line, it was just there as filler to test that i was getting threads working properly, then i was going to replace it with what i actually wanted to happen...i'm kind of learning as i go along with his.
[url=http://java.sun.com/docs/books/tutorial/essential/threads/]Threads: Doing Two or More Tasks at Once[/url]
> with the "extending" of classes
[url=http://java.sun.com/docs/books/tutorial/java/concepts/index.html]Object-Oriented Programming Concepts[/url]
As you are expected to know threads, it would be a far guess that you should have a basic grasp of OO concepts.
With that in mind, I think you should start looking at cutting the program down into classes. Your problems steam from the lack of OO.
Take a look at your problem statement, and look for keywords that could indicate potental objects within your system, and what methods (actions) each object would have.
mlka at 2007-7-13 19:37:25 >

Oh Jesus...And i was doing so well cobbling stuff together up to this point.
I do understand OO concepts, but don't always see the need to seperate them out...
for example in this program, the "car" would be considered an object...but the car doesn't really do anything it is just moved by 1 increment every iteration...
To save me from a re-write (which i really don't have time todo) is there any easy way i can get two concurrent threads working in the code i have?
right, re-read the java guide as you said (read it about 10 times before...) finnaly clicked...not really too sure where to start, but will have a go from the ground up.thanks for your help on this, will probably post up another thread in a day or so when i get back stuck on
> I do understand OO concepts, but don't always see the
> need to seperate them out...
So that you can design, code, and test the different pieces independently, rather than having a big, intertwined glob, and so that the entities in the code more closely reflect the entities in your real domain, which again makes the code easier to write, debug, and maintain.
jverda at 2007-7-20 23:37:37 >

Assuming that you won't have a lot of cars and traffic lights objects, and/or that you don't care about it being perfectly efficient, the easiest and probably cleanest solution is to give traffic lights and cars each their own thread. The traffic lights and cars objects would be completely responsible for their own thread; other objects wouldn't even be aware of them.
So maybe you'd have a objects with methods like (in pseudo code):public class TrafficLight
constructor
start // starts the light rotating through its states; creates a new thread as necessary
stop // stops the lights from changing; probably also sets the thread reference to null
getState // returns one of red,green,yellow
public class Car
constructor
start // also creates a thread
stop // also stops the thread
getPosition // maybe; actually it's an interesting question what a car object should do