Created
August 15, 2012 21:03
-
-
Save 2sbsbsb/3363671 to your computer and use it in GitHub Desktop.
TestRunner is a simple java program with two threads and executing a common method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
public class TestRunner { | |
// Set the running time here | |
public static long RUNNING_TIME_MIN = 10; | |
class Runnable1 implements Runnable { | |
public void myMethod1() { | |
System.out.println("I am " + Thread.currentThread().getName() + " invoking " + " myMethod1"); | |
} | |
@Override | |
public void run() { | |
long currentTime = System.currentTimeMillis(); | |
long loopFor = currentTime + RUNNING_TIME_MIN * 60 * 1000; | |
while (currentTime < loopFor) { | |
commonMethod(); | |
myMethod1(); | |
} | |
} | |
} | |
class Runnable2 implements Runnable { | |
public void myMethod2() { | |
System.out.println("I am " + Thread.currentThread().getName() + " invoking " + " myMethod2"); | |
} | |
@Override | |
public void run() { | |
long currentTime = System.currentTimeMillis(); | |
long loopFor = currentTime + RUNNING_TIME_MIN * 60 * 1000; | |
while (currentTime < loopFor) { | |
commonMethod(); | |
myMethod2(); | |
} | |
} | |
} | |
/** | |
* It creates two thread and starts | |
*/ | |
public void startWorking() { | |
Thread t1 = new Thread(new Runnable1(), "FirstThread"); | |
Thread t2 = new Thread(new Runnable2(), "SecondThread"); | |
t1.start(); | |
t2.start(); | |
} | |
/** | |
* Common method invoked by both threads | |
*/ | |
public void commonMethod() { | |
System.out.println("I am " + Thread.currentThread().getName() + " invoking " + " commonMethod"); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
TestRunner tRunner = new TestRunner(); | |
tRunner.startWorking(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment