Results 1 to 5 of 5
  1. #1
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Threads - What's wrong with my code?

    Here is what I am trying to do:
    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.

    I already finished the single-threaded program but am having trouble with the multi-threaded one. I have three classes; one class for odd numbers, one for even numbers, and one to execute the code. Here is my code so far:

    Even Numbers:
    Java Code:
    public class EvenNumbers extends Thread {
    	public void run() {
    		for (int i=1; i<=30; i++) {
    			if (i%2 == 0) {
    				System.out.println("Even number " + i);
    			}
    		}
    	}
    }

    Odd Numbers:
    Java Code:
    public class OddNumbers extends Thread {
    	public void run() {
    		for(int i=1; i<=30; i++) {
    			if (i%2 != 0) {
    				System.out.println("Odd Number " + i);
    			}
    		}
    	}
    }

    Main Class:
    Java Code:
    public class MultiThread {
    	public static void main(String args[]) {
    		EvenNumbers even = new EvenNumbers();
    		OddNumbers odd = new OddNumbers();
    		System.out.println("Even Numbers:");
    		even.start();
    		System.out.println("Odd Numbers:");
    		odd.start();
    	}
    }
    Unfortunately, my output ends up looking a little weird:

    Java Code:
    C:\Users\REDACTED\Dropbox\Schoolwork\REDACTED\Java Programming\Unit 5 - Exception
     Handling, Assertions\Program - Thread>java MultiThread
    Even Numbers:
    Odd Numbers:
    Even number 2
    Even number 4
    Odd Number 1
    Even number 6
    Odd Number 3
    Even number 8
    Even number 10
    Even number 12
    Even number 14
    Odd Number 5
    Odd Number 7
    Even number 16
    Odd Number 9
    Even number 18
    Odd Number 11
    Even number 20
    Odd Number 13
    Even number 22
    Odd Number 15
    Even number 24
    Even number 26
    Odd Number 17
    Even number 28
    Even number 30
    Odd Number 19
    Odd Number 21
    Odd Number 23
    Odd Number 25
    Odd Number 27
    Odd Number 29
    
    C:\Users\REDACTED\Dropbox\Schoolwork\REDACTED\Java Programming\Unit 5 - Exception
     Handling, Assertions\Program - Thread>
    Anyone got any ideas?

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Threads - What's wrong with my code?

    Explain "weird"..

  3. #3
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Re: Threads - What's wrong with my code?

    I wanted it to print out all of the even numbers at once, then all of the odd numbers. EX (with 10 as maximum number):
    Even number 2
    Even number 4
    Even number 6
    Even number 8
    Even number 10
    Odd number 1
    Odd number 3
    Odd number 5
    Odd number 7
    Odd number 9

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Threads - What's wrong with my code?

    Do you understand that left alone, threads run asynchronously with respect to each other? And if this is a school exercise, I would tend to think it demonstrates the behavior of threads the way it is. Each time you run this the output may actually be different, which is not weird but expected.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Re: Threads - What's wrong with my code?

    Oh okay. I forgot that threads pretty much run at the same time. Thanks!

Similar Threads

  1. Is it possible to execute the code by multiple threads simultaneously?
    By Azuki in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-02-2013, 06:35 PM
  2. What will happen when other threads use the following code?
    By Azuki in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-02-2013, 03:44 PM
  3. What is wrong with my code?
    By tweetychirpy in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-23-2011, 05:16 PM
  4. What is wrong with that code ?!
    By qwerty53 in forum New To Java
    Replies: 5
    Last Post: 07-12-2011, 01:34 PM
  5. What's wrong with my code ?
    By TheTypicalStudent in forum New To Java
    Replies: 12
    Last Post: 03-28-2011, 11:05 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
  •