Monday, February 15, 2010

Executing some jobs in parallel

import java.util.*;
import java.io.*;

public class Test {

public static void main (String[] args) {
Runnable r = new Runnable () {

public void run () {
while (true) {
try {
System.out.println ("-- this lines are being printed in another thread.."+Thread.currentThread().getName());
Thread.currentThread().sleep(130);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};

new Thread (r).start();
while (true) {
System.out.println (" -- this lines are bing printed in main thread.."+Thread.currentThread().getName());
try {
Thread.currentThread ().sleep (60);
} catch (InterruptedException ex) {
//noop
}
}
}
}

-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0

No comments:

Post a Comment