Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: threads

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default threads

    i know how to make a thread with extends thread and all blah blah blah, but i was just trying different things and i could never get the .wait() and .sleep() functions to work. so i tried to figure it out and it was something about synchronizing?!? and all the examples i found were pretty ehh, so i was wondering if a real person could put it into words for me because books and websites tend to overexplain and make things more confusing. Or if someone has a real good source for learning about threading that would be great


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: threads

    For what its worth, its better practice to implement Runnable as opposed to extending Thread.

    Thread.sleep is static, so you typically call it in a static way to pause the current thread for a period of time.
    try{
        Thread.sleep(1000);//sleeps the current thread for 1 second.
    }catch(Exception e){
     
    }

    wait() is called on an object, which results in the current thread to wait, either specified by a millisecond parameter, or indefinitely. To wake up said thread, another thread can call notify() on the particular object it wishes to wake up (the one wait was called on).

    	public static class Test1 implements Runnable{
    	    private Test2 test2;
    	     public Test1(){
    	         test2 = new Test2();
    	        (new Thread(test2)).start();
    	        (new Thread(this)).start();
    	     }
    	    public void run(){
    	        try{
    	                Thread.sleep(2000);
    	                 synchronized(test2){
    	                        test2.notify();
    	                }
    	        }catch(Exception e){
    	        }
    	    }
    	}
     
    	public static class Test2 implements Runnable{
    	     public void run(){
    	        System.out.println("In Test2");
    	        try{
    	        	synchronized(this){
    	        		this.wait();
    	        	}
    	        }catch(Exception e){
    	        	e.printStackTrace();
    	        }
    	        System.out.println("Awake");
    	    }
    	}
    	public static void main(String[] args){
    		new Test1();
    	}

    See Concurrency for detailed expainations.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: threads

    Moved to - Threads - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. newbie help threads
    By fortune2k in forum Threads
    Replies: 19
    Last Post: October 20th, 2010, 03:40 PM
  2. How Can I run java threads
    By Saeid in forum Threads
    Replies: 12
    Last Post: August 6th, 2010, 03:12 PM
  3. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 11:21 AM
  4. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 07:28 AM