Results 1 to 8 of 8

Thread: Threads

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Threads

    Single thread means just a normal program right? It's just normal. ie
    Java Code:
    class HelloWorld{
    public static void main(String [] args){
    System.out.println("hi");
    }
    }
    But multiple is different how?



    Is this an example of multiplethreading?
    Java Code:
    public class Threading extends Thread {
    	public void run(){
    		for(int i = 0; i <= 30; i++){
    			System.out.println(i);
    		}
    	}
    	public static void main(String [] args){
    		new Threading().start();
    	}
    }
    Last edited by Army; 05-01-2012 at 04:19 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Threads

    This is the first result for googling "java threading": Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Threads

    I know, I was already looking at it. Is there any difference in the first one and the second one that i posted?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Threads

    Quote Originally Posted by Army View Post
    I know, I was already looking at it. Is there any difference in the first one and the second one that i posted?
    What happens when you run each? To further demonstrate threading, you might try something more like this (not tested, may contain compiler errors):

    Java Code:
    public class Threading extends Thread {
        int n;
        public Threading(int n){
            this.n = n;
        }
    
        public void run(){
            for(int i = 0; i <= 1000; i++){
                System.out.println("Thread " + n + " executing.");
                System.out.println(i);
                System.out.println("Thread " + n + " done executing. Sleeping now...");
                Thread.sleep(Math.random() * 5000);
            }
        }
        public static void main(String [] args){
            new Threading(1).start();
            new Threading(2).start();
            new Threading(3).start();
        }
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Threads

    Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
    ^That is the question I was asked. The second code is the one I thought had implemented multi-threading.

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Threads

    Java Code:
    public class Threading extends Thread {
        int n;
        public Threading(int n){
            this.n = n;
        }
     
        public void run(){
            for(int i = 0; i <= 30; i++){
                System.out.println("Thread " + n + " starting.");
                System.out.println(i);
                System.out.println("Thread " + n + " done starting. Sleeping now...");
                try {
    				Thread.sleep(5000);
    			} catch (InterruptedException e) {
    				
    				System.out.println("Thread " + n + "was not finished.");
    			}
            }
        }
        public static void main(String [] args){
            new Threading(1).start();
            new Threading(2).start();
            new Threading(3).start();
        }
    }
    After some tweaking I did this, but then it does 3 threads all printing the same thing. How would I start to make it where as if one thread is printing the same number then the thread with the same number would sleep.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Threads

    Quote Originally Posted by Army View Post
    Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
    ^That is the question I was asked. The second code is the one I thought had implemented multi-threading.
    By that definition, I guess you technically accomplished the goal. But I would expect something is missing from the description. Either that or it's a bit of a lame example of threading, imho.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Threads

    Quote Originally Posted by Army View Post
    After some tweaking I did this, but then it does 3 threads all printing the same thing. How would I start to make it where as if one thread is printing the same number then the thread with the same number would sleep.
    I meant that as a basic example of what threads do, not as an answer to your assignment. Did you read through the tutorial I linked?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  2. Help with Threads
    By gicp89 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-01-2011, 09:00 PM
  3. Threads and UML
    By JUser in forum Advanced Java
    Replies: 0
    Last Post: 09-27-2010, 08:43 PM
  4. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 08:39 PM
  5. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM

Posting Permissions

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