Results 1 to 12 of 12

Thread: [RESOLVED] Threading in Java to display numbers.

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [RESOLVED] Threading in Java to display numbers.

    Hi all,

    I want to print some numbers, say starting from 1 and increment by 1, in a specific interval, say each one second.

    I want to use Java thread to do this. Can you guys give me a clue.

    I've try this, but I can't execute on main(). When I call the init() method, thread stopped.

    Code:
    public class Counter extends Applet implements Runnable {
    	Thread t;	
    	int Count;
    
        @Override
    	public void init()	
    	{	
    		Count=0;
    		t=new Thread(this);
    		t.start();
    	}
    
        @Override
    	public boolean mouseDown(Event e,int x, int y)
    	{	
    		t.stop();
    		return true;
    	}
    
    	public void run()
    	{
    		while(true)
    		{
    			Count++;
    			repaint();
    			try {
    				Thread.sleep(10);
    			} catch (InterruptedException e) {}
    		}
    	}
    
        @Override
    	public void paint(Graphics g)
    	{
    		//g.drawString(Integer.toString(Count),10,10);
    		System.out.println("Count= "+Count);
    	}
    
        @Override
    	public void stop()
    	{
    		t.stop();
    	}
    }
    
    public class Main {
        public static void main(String[] args) {
             
            Counter ss = new Counter();
            ss.run();
        }
    
    }
    Thanks.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threading in Java to display numbers.

    Try this
    Code:
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Util {
    
        public static void main(String[] args) throws Exception {
    	Thread th = new Thread(new Runnable() {
    
    	    public void run() {
    		int i = 0;
    		while (i <= 10) {
    		    try {
    			System.out.println(i++);
    			Thread.sleep(1000);
    		    } catch (InterruptedException ex) {
    			Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    		    }
    		}
    	    }
    	});
    	th.start();
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threading in Java to display numbers.

    And you can't use the main method to display an applet, you must have a containing browser. so put it in an HTML page
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Oh dear, it's a terrible mistake I have make. Yes you are right, and mess-up on my IDE I think.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Quote Originally Posted by ComputerJy
    Try this
    Code:
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Util {
    
        public static void main(String[] args) throws Exception {
    	Thread th = new Thread(new Runnable() {
    
    	    public void run() {
    		int i = 0;
    		while (i <= 10) {
    		    try {
    			System.out.println(i++);
    			Thread.sleep(1000);
    		    } catch (InterruptedException ex) {
    			Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    		    }
    		}
    	    }
    	});
    	th.start();
        }
    }

    Thanks pal.

    This code working as I expected. I've implement it on a button click-event and it works fine. But mess up one thing on my GUI.

    Until the thread execution completed, all the controls(I mean buttons, check boxes, etc) are freeze. That mean I can't click any button, check-uncheck a check box and so on. Not the whole frame, I can move frame only.

    What can be wrong.

    After execution of the thread completed, I can use all controls.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threading in Java to display numbers.

    Thread.sleep(1000); causes the main thread to suspend all execution for 1000 milliseconds
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Yes, initially I guess that do something like this in that thread.

    Code:
            int x = 1;
    	Thread th = new Thread(new Runnable() {
    
    	    public void run(){
    		int counter = 0;
    		while(counter <= 10000){ 
    			System.out.println(counter++);
    		}
                }
    	});
    But it not work. As I said early, until print the number 10000 to console, I can't use any controls on the GUI.

    Any idea pal...
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threading in Java to display numbers.

    Actually something is wrong with the way you're organizing your code. Because a simple test
    Code:
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Util {
    
        public static void main(String[] args) throws Exception {
    	Thread th = new Thread(new Runnable() {
    
    	    public void run() {
    		int i = 0;
    		while (i <= 10) {
    		    try {
    			System.out.println(i++);
    			Thread.sleep(1000);
    		    } catch (InterruptedException ex) {
    			Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    		    }
    		}
    	    }
    	});
    	th.start();
    	Thread.sleep(500);
    	for (char c = 'a'; c <= 'z'; c++) {
    	    System.out.println(c);
    	}
        }
    
        private Util() {
        }
    }
    proves that if you call Thread.sleep the current thread only will be suspended not the whole system.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Ok, I try what you say. But I got an error on following lines. Actually I cant use the thread th again.

    Code:
    th.start();
    Error: package th does not exit

    Code:
    Thread.sleep(500);
    Error: illegal start of type


    I used the thread in the button click event as follows.

    Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            Util appProcess = new Util();
            appProcess.th.run();        
        }

    But still the same thing is happened. All those things going on two separate classes right now, as you can see I have create instance for the Util class where I have implement my thread.
    Last edited by eranga262154; Mar 12th, 2008 at 10:45 PM.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  10. #10

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Ok pal, I have observed one thing here. I simply create a thread in the same class where I create GUI. Here is my thread.

    Code:
            Thread testTH = new Thread(new Runnable() {
    
    	    public void run(){
    		int counter = 0;
    		while(counter <= 10){ 
    		    try {
    			System.out.println(counter++);
    			Thread.sleep(1000);
    		    } 
                        catch (InterruptedException ex) {
    			 System.out.println(ex);
    		    }
    		}
                }
       });
    Then in the click-event I simply call the start() of the thread.

    Code:
    testTH.start();
    It works fine. So what is wrong with my code.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threading in Java to display numbers.

    I don't know
    I haven't seen your code!!
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Threading in Java to display numbers.

    Ya it's true.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

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



Click Here to Expand Forum to Full Width