I was developing the code of creating a thread but without extending the thread class or implementing the runnable interface , that is through anonymous inner classes ..
Runnable run = new Runnable() {
public void run() {
try {
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
System.out.print(i + "\n" + "..");
}
} catch (InterruptedException e) {
System.out.println(" interrupted");
}
}
};
new Thread(run).start();
new Thread(run).start();
Don't wait for one to finish before starting the second or you have three threads where on one is ever running (in which case the additional threads were pointless)
BTW: Your synchronized isn't doing anything useful but it could cause the Thread to function incorrectly.
Anonymous inner classes are the classes without a name, means there is no explicit name for it, but JVM name it as Mythread3$1 for referencing its objects. so when you print th.getClass() and th.getClass().getSuperclass() you will get output as MyThread3$1 and Thread.
you can create Anonymous inner classes by extending Thread class or any of its subclasses (the other implementing Runnable interface or any of its subtypes). In the first piece of code, you extended thread class. That's why you got class name as MyThread3$1( as it is Anonymous inner class) and superclass as Thread class( as you extended it). so you can create as many anonymous inner classes extending a thread class and JVM names them as MyThread3$1, MyThread3$2, MyThread3$3...... But when you call start method each thread will execute their run method only(which you overrode in MyThread3$1, MyThread3$2 by extending thread class).
package com.tej.threads;
public class MyThread3 {
public static void main(String... a) {
Thread th = new Thread() {
public synchronized void run() {
for (int i = 0; i < 20; i++) {
try {
System.out.println(i + "\t" + ".." + Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Thread th1 = new Thread() {
public synchronized void run() {
for (int i = 50; i < 70; i++) {
try {
System.out.println(i + "\t" + ".."+ Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
th.start();
try {
th.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
th1.start();
System.out.println(th.getClass()+ " " + th.getClass().getSuperclass());
for(int i=0;i<20;i++)
{
System.out.println("i am main Thread");
}
}
}
output is
0 ..8
1 ..8
2 ..8
3 ..8
4 ..8
5 ..8
6 ..8
7 ..8
8 ..8
9 ..8
10 ..8
11 ..8
12 ..8
13 ..8
14 ..8
15 ..8
16 ..8
17 ..8
18 ..8
19 ..8
class com.tej.threads.MyThread3$1 class java.lang.Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
i am main Thread
50 ..9
51 ..9
52 ..9
53 ..9
54 ..9
55 ..9
56 ..9
57 ..9
58 ..9
59 ..9
60 ..9
61 ..9
62 ..9
63 ..9
64 ..9
65 ..9
66 ..9
67 ..9
68 ..9
69 ..9
you can clearly see thread1 will print 1 to 20 with its id, Thread2 will print 50 to 70 with its id that means each thread is executing its own run method. Note: The main thread will execute program line by line if it encounters a th.start method then main thread will send child thread to execute its run method and main thread goes to next line for execution.
The fact that you declare two anonymous inner classes extending Thread and overriding the run() method is not an issue in itself. We may consider not really readable but there is no issue.
However, you should consider using the Runnable
interface. You should separate the processing/algorithms and the Threading policy. So it would be better to have something like this:
public class ThreadLauncher {
public static void main(String[] args) {
Thread job1 = new Thread(new Job1());
Thread job2 = new Thread(new Job2());
job1.start();
job2.start();
}
}
public class Job1 implements Runnable {
@Override
public void run() {
// Do some stuff
}
}
public class Job2 implements Runnable {
@Override
public void run() {
// Do some other stuff
}
}
This allows you to launch several time the same job, for example.
If you want to take it one step further, you could consider using ThreadPoolExecutor to handle your Threading strategy.