Results 1 to 13 of 13
  1. #1
    runn81 is offline Member
    Join Date
    May 2011
    Location
    Warsaw, Poland
    Posts
    6
    Rep Power
    0

    Default 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();
    		    }
    	  }
    }
    and this is class that supouse to control it:

    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);
    			  }
    			}
    the problem is i cannot access methods stopThread, suspendThread and resumeThread from class Liczby and I am stuck dunno what to do. I tried to run liczby as object of cass Liczby instead Thread but this way i cannot run it at the some time with panel to control it. Please help! I know if class Liczby was extending Thread would be no problem, cheers.
    Last edited by runn81; 05-08-2011 at 11:33 PM. Reason: add [code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  3. #3
    runn81 is offline Member
    Join Date
    May 2011
    Location
    Warsaw, Poland
    Posts
    6
    Rep Power
    0

    Default

    thx for telling me and i'm sorry as i am new to forum

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Sunde, how do you show what a code tag looks like without it being interpreted as a code tag?

  6. #6
    runn81 is offline Member
    Join Date
    May 2011
    Location
    Warsaw, Poland
    Posts
    6
    Rep Power
    0

    Default

    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();
    these methods basically change boolean from true to false

    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
    when i copied the code into different class that extends Thread there was no errors.

    thx
    Last edited by runn81; 05-09-2011 at 12:01 AM.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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)
    This line is the problem:

    Java Code:
    liczby = new Liczby();
    Because the type of the variable liczby is not Liczby. It's Thread. Your Liczby is anonymous:

    Java Code:
    Thread liczby = new Thread(new Liczby());
    Instead, perhaps you should do this:

    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.

  8. #8
    runn81 is offline Member
    Join Date
    May 2011
    Location
    Warsaw, Poland
    Posts
    6
    Rep Power
    0

    Default

    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)
    basically it is very similiar problem.
    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by kjkrum View Post
    Sunde, how do you show what a code tag looks like without it being interpreted as a code tag?
    Quote Sunde's post to see that.

    db

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Quote Originally Posted by runn81 View Post
    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)
    basically it is very similiar problem.
    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.
    Those methods don't exist in the class Thread. You need to call those methods on an object of class Liczby.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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]

  12. #12
    runn81 is offline Member
    Join Date
    May 2011
    Location
    Warsaw, Poland
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    Those methods don't exist in the class Thread. You need to call those methods on an object of class Liczby.
    thx 4 ur response

    How do I do it? I tried and ultimately failed big time :)

  13. #13
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

Similar Threads

  1. All Threads in Blocked State ( Program Hangs )
    By thehellmaker in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-09-2011, 01:08 AM
  2. Replies: 2
    Last Post: 02-14-2011, 03:27 PM
  3. Using threads in a graphical program
    By jameskelly in forum New To Java
    Replies: 6
    Last Post: 11-28-2010, 04:52 PM
  4. Replies: 4
    Last Post: 09-30-2009, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •