嗨,我在我的程序中运行多个线程,每个线程包含 6 个元素的数组。我在线程的运行方法中添加了同步,但只有第一个线程是同步的,以从 1 到 6 的顺序显示数组。
/* My run method for the thread */
public synchronized void run(){
int from = St7.stt[acnt].getCf();
int to = St7.stt[acnt].getCt();
int inc = St7.stt[acnt].getCi();
threadsRunning++; // We now have more threads
this.threadId = threadsRunning;
active = true;
try{
int loop = from;
while (active && loop <= to){
System.out.println(text + " Counter: " + St7.stt[acnt].getCounter() + ".");
System.out.println();
Thread.sleep(sleep);
loop=loop+inc;
}