1 will create a new object of type ClassA for every thread
the second will not and look at the difference !
package threadsleep;
public class Main {
public static void main(String[] args) {
// 1st version new object ClassA of every thread
//ClassA ca = new ClassA();
//Thread thread;
for(int i=0; i < 10; i++) {
//thread = new Thread(ca);
//thread.start();
new Thread(new ClassA()).start();
}
}
}
run:
Thread-0
a : Thread-0 sleeping 5 seconds...
Thread-1
a : Thread-1 sleeping 5 seconds...
Thread-3
a : Thread-3 sleeping 5 seconds...
Thread-5
a : Thread-5 sleeping 5 seconds...
Thread-7
a : Thread-7 sleeping 5 seconds...
Thread-6
a : Thread-6 sleeping 5 seconds...
Thread-2
a : Thread-2 sleeping 5 seconds...
Thread-4
a : Thread-4 sleeping 5 seconds...
Thread-8
a : Thread-8 sleeping 5 seconds...
Thread-9
a : Thread-9 sleeping 5 seconds...
a : Thread-0-Thread-0 after sleeping 5 seconds...
a : Thread-1-Thread-1 after sleeping 5 seconds...
a : Thread-5-Thread-5 after sleeping 5 seconds...
a : Thread-3-Thread-3 after sleeping 5 seconds...
a : Thread-7-Thread-7 after sleeping 5 seconds...
a : Thread-6-Thread-6 after sleeping 5 seconds...
a : Thread-2-Thread-2 after sleeping 5 seconds...
a : Thread-4-Thread-4 after sleeping 5 seconds...
a : Thread-9-Thread-9 after sleeping 5 seconds...
a : Thread-8-Thread-8 after sleeping 5 seconds...
BUILD SUCCESSFUL (total time: 10 seconds)
The instance variable "a" was not modified because for every thread there is a different "a" !
public class Main {
public static void main(String[] args) {
// 2nd version the thread will share the same object
ClassA ca = new ClassA();
Thread thread;
for(int i=0; i < 10; i++) {
thread = new Thread(ca);
thread.start();
//new Thread(new ClassA()).start();
}
}
}
run:
Thread-0
a : Thread-0 sleeping 5 seconds...
Thread-1
a : Thread-1 sleeping 5 seconds...
Thread-2
a : Thread-2 sleeping 5 seconds...
Thread-3
a : Thread-3 sleeping 5 seconds...
Thread-5
a : Thread-5 sleeping 5 seconds...
Thread-7
a : Thread-7 sleeping 5 seconds...
Thread-9
a : Thread-9 sleeping 5 seconds...
Thread-4
a : Thread-4 sleeping 5 seconds...
Thread-6
a : Thread-6 sleeping 5 seconds...
Thread-8
a : Thread-8 sleeping 5 seconds...
a : Thread-8-Thread-0 after sleeping 5 seconds...
a : Thread-8-Thread-1 after sleeping 5 seconds...
a : Thread-8-Thread-2 after sleeping 5 seconds...
a : Thread-8-Thread-5 after sleeping 5 seconds...
a : Thread-8-Thread-3 after sleeping 5 seconds...
a : Thread-8-Thread-7 after sleeping 5 seconds...
a : Thread-8-Thread-9 after sleeping 5 seconds...
a : Thread-8-Thread-4 after sleeping 5 seconds...
a : Thread-8-Thread-6 after sleeping 5 seconds...
a : Thread-8-Thread-8 after sleeping 5 seconds...
BUILD SUCCESSFUL (total time: 11 seconds)
The thread "8" had a big influence on all the others threads because the thread "8" modified the instance variable "a" ! In fact it is because this poor thread "8" was the last to be started and as he is the last, he modified the same "a" instance variable for all the threads !!!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package threadsleep;
public class ClassA implements Runnable {
String a;
public void run() {
Thread currenthread = Thread.currentThread();
a = currenthread.getName();
System.out.println(a);
try {
System.out.println("a : " + a + " sleeping 5 seconds...");
currenthread.sleep(10000);
System.out.println("a : " + a + "-" + currenthread.getName()+ " after sleeping 5 seconds...");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
You can share the same objects. You will avoid to waste memory... But if your threads modify the data... You have to find a way keep the operations/data modifications consistent... In other words all the operations you do that can corrupt the data have to be atomic to avoid the corruption...
- Rudy -
No comments:
Post a Comment