Use of Java Threads
Using thread run same method multiple times.
My requirement is to run below method multiple times. The number of times the method should be run depends on the number of times the class creates a new instance
public void counter() { for (int i = 0; i < 6; i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Running the above method multiple times dynamically
package com.bmc.arsystem.sr; public class Sandbox { public static void main(String[] args) { // TODO Auto-generated method stub multiProcess(); } public static void multiProcess() { Thread th[] = new Thread[4]; for (int i = 0; i < th.length; i++) { th[i] = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Threads instance = Threads.class.newInstance(); instance.counter(); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); th[i].start(); } } }
Implementation Example
package thread;
public class Thread01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread01().thread01();
new Thread01().thread02();
new Thread01().nonthread01();
new Thread01().nonthread02();
}
public void thread01() {
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i;
for (i = 0; i <= 10; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable, "thread one");
thread.start();
}
public void thread02() {
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i;
for (i = 0; i <= 10; i++) {
System.out.println("a" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable, "thread one");
thread.start();
}
public void nonthread01() {
for (int i = 0; i <= 10; i++) {
System.out.println("a" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void nonthread02() {
for (int i = 0; i <= 10; i++) {
System.out.println("b" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2nd way of implementation package thread;
public class Thread02 {
static String name1;
static String name2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1 = new Thread(new T1());
t1.start();
name1 = t1.getName();
Thread t2 = new Thread(new T2());
t2.start();
name2 = t2.getName();
}
}
class T1 extends Thread02 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
int i;
for (i = 0; i <= 10; i++) {
System.out.println(i);
System.out.println(name1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class T2 extends Thread02 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
int i;
for (i = 0; i <= 10; i++) {
System.out.println(i);
System.out.println(name2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
No comments:
Post a Comment