I want to make an object in a new thread, that will create another thread. In that last created threads I need a reference for variable from exact object that created this thread. So far I have this code:
Main
public static void main(String[] args) {
for(int i = 0; i < 2; i++){
ClassThread ct = new ClassThread();
Thread t = new Thread(ct);
t.start();
}
}
ClassThread
public static volatile int liczba = 1;
private static int id = 0;
@Override
public void run() {
for(int i = 0; i < 2; i++){
Class1 cla = new Class1(liczba, id);
Thread t = new Thread(cla);
t.start();
id++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Class1
private volatile int var;
private int id;
public Class1(int _var, int _id) {
var = _var;
id = _id;
}
@Override
public void run() {
while(true){
System.out.println("Thread: " + id + " : " + var);
var++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}