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
Solved

java 6: write two thread

Posted on 2012-04-01
1
405 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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
eclipse shortcuts 9 62
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 56
Tagging and Merging on Branch 1 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

791 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