Results 1 to 13 of 13
- 05-08-2011, 10:54 PM #1
Member
- Join Date
- May 2011
- Location
- Warsaw, Poland
- Posts
- 6
- Rep Power
- 0
problems with threads in my program (runnable)
hey all!
I've got a problem with my homework I cannot get past throu I am quite sure that it is very simple one and somebody will be able to help me.
I have to write a program that starts and controls thread from diffrent class that implements runnable.
this is class with a thread i've got:
Java Code:public class Liczby implements Runnable { boolean stopped = false; boolean suspended = false; public void run() { int iii = 0; while(!stopped) { while (!suspended) { try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(iii); iii++; } } if (stopped) { System.out.println(iii); } if (suspended) { try { synchronized(this) { wait(); } } catch (Exception e) { System.err.println("Exception w wait"); } System.out.println("Watek wstrzymany na wartosci " + iii); } } public void start() { run(); } public void interrupt() { interrupt(); } public void stopThread() { stopped = true; } public void suspendThread() { suspended = true; } public void resumeThread() { suspended = false; synchronized(this) { notify(); } } }
Java Code:import javax.swing.JOptionPane; public class LiczbyKontroler { /** * @param args */ public static void main(String args[]) { String msg = "I = interrupt\n" + "E = end\n" + "S = suspend\n" + "R = resume\n" + "N = new start"; Thread liczby = new Thread(new Liczby()); liczby.start(); String cmd; while ((cmd = JOptionPane.showInputDialog(msg)) != null) { char c = cmd.charAt(0); switch (c) { case 'I' : liczby.interrupt(); break; case 'E' : liczby.stopThread(); break; case 'S' : liczby.suspendThread(); break; case 'R' : liczby.resumeThread(); break; case 'N' : if (liczby.isAlive()) JOptionPane.showMessageDialog(null, "Thread alive!!!"); else { liczby = new Liczby(); liczby.start(); } break; default : break; } } System.exit(0); } }
Last edited by runn81; 05-08-2011 at 11:33 PM. Reason: add [code]
- 05-08-2011, 11:25 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
You will get a lot more people helping you if you wrap your code in code tags. It makes the code much easier to read.
To do this type [code] before any code, then paste your code in, then put [/code] at the end of the code
[code]
YOUR CODE HERE
[/code]
It will look like this
Java Code:YOUR CODE HERE
- 05-08-2011, 11:32 PM #3
Member
- Join Date
- May 2011
- Location
- Warsaw, Poland
- Posts
- 6
- Rep Power
- 0
thx for telling me and i'm sorry as i am new to forum
- 05-08-2011, 11:33 PM #4
First, please post your code within code tags.
Second, post the exact errors you are getting.
Third, I'm really not clear what you're asking, but your choice of method names is worrying. You should never use Thread.stop or Thread.suspend. Here's why. You do seem to be following the advice under "What should I use instead of Thread.stop?", but you need to synchronize access to your 'stopped' and 'suspended' variables.
- 05-08-2011, 11:34 PM #5
Sunde, how do you show what a code tag looks like without it being interpreted as a code tag?
- 05-08-2011, 11:41 PM #6
Member
- Join Date
- May 2011
- Location
- Warsaw, Poland
- Posts
- 6
- Rep Power
- 0
i will try to explain it as clear as i can :)
class Liczby has got methods:
Java Code:public void stopThread() { stopped = true; } public void suspendThread() { suspended = true; } public void resumeThread() { suspended = false; synchronized(this) { notify();
and when I am in class LiczbyKontroler and I am trying to stop thread called "liczby" by method "stopThread" from class Liczby:
Java Code:case 'E' : liczby.stopThread(); break; case 'S' : liczby.suspendThread(); break; case 'R' : liczby.resumeThread(); break;
i get this:
Java Code:The method resumeThread() is undefined for the type Thread Type mismatch: cannot convert from Liczby to Thread at LiczbyKontroler.main(LiczbyKontroler.java:25)
basically if class Liczby was extending Thread there would be no problem whatsoever but my teacher wants the class Liczby to be:
Java Code:public class Liczby implements Runnable
thxLast edited by runn81; 05-09-2011 at 12:01 AM.
- 05-08-2011, 11:58 PM #7Java Code:
The method resumeThread() is undefined for the type Thread Type mismatch: cannot convert from Liczby to Thread at LiczbyKontroler.main(LiczbyKontroler.java:25)
Java Code:liczby = new Liczby();
Java Code:Thread liczby = new Thread(new Liczby());
Java Code:Liczby liczby = new Liczby(); Thread thread = new Thread(liczby);
Your methods that change the booleans need to be synchronized. The loop conditions that read those booleans also need to be synchronized. Synchronization in Java is not just about atomicity or exclusivity. Changes to variables are not guaranteed to be visible to other threads unless the reads and writes are synchronized.
- 05-09-2011, 12:49 AM #8
Member
- Join Date
- May 2011
- Location
- Warsaw, Poland
- Posts
- 6
- Rep Power
- 0
cheers for help good people of java-forums but i am afraid:
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method stopThread() is undefined for the type Thread The method suspendThread() is undefined for the type Thread The method resumeThread() is undefined for the type Thread at LiczbyKontroler.main(LiczbyKontroler.java:26)
this seriously frustrates me cous i spent to much time on this thing already and it is after midnight and i cant sleep cous of that. and i am sure it is trivial thing that needs to be changed! nothing come tos me mind i will have to sleep it over and start tomorow. thx again.
- 05-09-2011, 12:56 AM #9
- 05-09-2011, 04:04 AM #10
- 05-09-2011, 04:11 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
As darryl said, if you quote my post you will see I actually put before the code. So it looks something like this(without spaces of course
[[i][ /i]code]
YOUR CODE HERE
[/code]
- 05-09-2011, 12:49 PM #12
Member
- Join Date
- May 2011
- Location
- Warsaw, Poland
- Posts
- 6
- Rep Power
- 0
- 05-09-2011, 06:26 PM #13
Similar Threads
-
All Threads in Blocked State ( Program Hangs )
By thehellmaker in forum Threads and SynchronizationReplies: 1Last Post: 08-09-2011, 01:08 AM -
How to release multiple threads from waiting state to runnable state
By Dayanand in forum New To JavaReplies: 2Last Post: 02-14-2011, 03:27 PM -
Using threads in a graphical program
By jameskelly in forum New To JavaReplies: 6Last Post: 11-28-2010, 04:52 PM -
Is there a way to speed up these Threads? (Rendering Problems)
By anro in forum Advanced JavaReplies: 4Last Post: 09-30-2009, 10:49 AM
Bookmarks