Solved

java 6: write two thread

Posted on 2012-04-01
1
403 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
groupNoAdj 7 87
How to simplify a groovy script that reads a WS because the result being sent back is simpler 1 47
expectj telnet failing 5 26
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)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now