Solved

java 6: write two thread

Posted on 2012-04-01
1
407 Views
Last Modified: 2012-04-01
hello all,

i would like to write a java program to create two threads. one will print even numbers and another will print odd numbers. Output should be the sequence of natural numbers. 1,2,3,4,5....

any suggestion?

thanks,

wantime
0
Comment
Question by:wantime
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37792497
public class TwoThreads {

    public static void main(String[] args) {
        PrintNumb n0 = new PrintNumb(0);
          PrintNumb n1 = new PrintNumb(1);
        Thread t0 = new Thread(n0);
         Thread t1 = new Thread(n1);
        t0.start();
        try{
        Thread.currentThread().sleep(1000);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        t1.start();





    }

}


class PrintNumb implements Runnable {

    int i;
  public PrintNumb(int i){
      this.i = i;
  }

    public void run() {

        while(i< 20){
            try{
            Thread.currentThread().sleep(2000);
            System.out.print(i +",");
            i+=2;
            }catch(Exception ex){
                ex.printStackTrace();
            }

        }

    }



}

Open in new window


Output:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question