Monday, June 30, 2014

Join,Start,Run

package Algorithm;

import org.junit.Test;
// executor framework eliminates the need for creating thread and calling it.It automatically handles it ,we  does not have to concern on it,as the code return below does not need to be write while working with thread using executor framework

/*The clear distinction can be seen in Run Vs Start.
 * In Start main method does not wait for Other threads run operation to complete.
 * It is totally asychronous.But,in run it is syschronous after completion of all threads operation main thread again resume its operation.
 * So,in case of start join can be used so that the thread waits until the operation of other thread is completed.
 *
 */

public class Basic {
private int i;
private int k;

public void SomeMethod1() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
i = 1;

}

};
separateTask1.run();
}

public void SomeMethod11() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
i = 1;

}

};
Thread thread = new Thread(separateTask1);
thread.start();

// thread.join();
}

public void SomeMethod2() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
k = 2;
}

}

};
separateTask1.run();

}

public void SomeMethod22() throws InterruptedException {

Runnable separateTask1 = new Runnable() {

@Override
public void run() {
for (int i = 0; i <= 1000000000; i++) {
k = 2;
}

}

};
Thread thread = new Thread(separateTask1);
thread.start();
thread.join();

}

@Test
public void Sum() throws InterruptedException {
SomeMethod11();
SomeMethod22();

System.out.println(i + k);
}

}

No comments:

Post a Comment