gpt4 book ai didi

java - 实现同步以获得正确的线程输出

转载 作者:行者123 更新时间:2023-12-01 12:26:05 24 4
gpt4 key购买 nike

当前,当我运行程序时,线程是随机运行的。例如当前输出是:

Global.sharedBuffer[0] = 2
Global.sharedBuffer[1] = 1
Global.sharedBuffer[2] = 1
Global.sharedBuffer[3] = 1
Global.sharedBuffer[4] = 1
Global.sharedBuffer[5] = 1
Global.sharedBuffer[6] = 1
Global.sharedBuffer[7] = 1
Global.sharedBuffer[8] = 1
Global.sharedBuffer[9] = 1
Global.sharedBuffer[10] = 2
Global.sharedBuffer[11] = 4
Global.sharedBuffer[12] = 3

我想要的是从sharedBuffer 0到9全1,然后从10 - 19全2等等。我添加了一个同步块(synchronized block),认为它会做到这一点,但是,它只是在每次线程启动时阻止它进行上下文切换叫。您如何实现这个?

代码:

import java.io.*;
import java.lang.*;
import java.util.*;

class MyThreadExample2 {
public static void main(String[] args) {
HelloThread ht1 = new HelloThread(1);
HelloThread ht2 = new HelloThread(2);
HelloThread ht3 = new HelloThread(3);
HelloThread ht4 = new HelloThread(4);
ht1.start();
ht2.start();
ht3.start();
ht4.start();
}
}

class Global {
public static int[] sharedBuffer = new int[1000];
public static int in = 0;
}


class HelloThread extends Thread {
int threadID;

HelloThread(int threadID) {
System.out.println("threadID: " + threadID);
this.threadID = threadID;
}

public synchronized void run() {
for (int i = 0; i < 100; i++) {



if((Global.in >= 0 || Global.in <= 99) && (this.threadID == 1))
Global.sharedBuffer[Global.in] = this.threadID;

else if((Global.in >= 100 || Global.in <= 199) && (this.threadID == 2))
Global.sharedBuffer[Global.in] = this.threadID;

else if((Global.in >= 200 || Global.in <= 299) && (this.threadID == 3))
Global.sharedBuffer[Global.in] = this.threadID;

else if((Global.in >= 300 || Global.in <= 399) && (this.threadID == 4))
Global.sharedBuffer[Global.in] = this.threadID;



System.out.println("Thread " + this.threadID + " has written "
+ this.threadID + " to Global.sharedBuffer[" + Global.in + "]\n");


Global.in++;
}

if (this.threadID == 4)
{
try {this.sleep(2000L);
}
catch (Throwable e) {e.printStackTrace();
}

System.out.println("The final buffer is **************\n");
for (int i = 0; i < 30; i++) {
System.out.println("Global.sharedBuffer[" + i + "] = " +
Global.sharedBuffer[i]);
} // for
} // if
} // run




} // end Thread

最佳答案

只有当您可以制定可以独立于其他任务执行的任务时,多线程才有效。您必须避免共享变量,如果无法避免,则必须适当保护访问,这通常意味着限制线程执行的并发性。

对于您的任务,制定独立任务很简单,因为每个线程应写入数组的不同区域:

public class ThreadingExample {
public static void main(String[] args) {
final int numThread=4, chunkSize=10;
int[] array=new int[numThread*chunkSize];
Thread[] thread=new Thread[numThread];

// create threads and define their jobs
for(int t=0, p=0; t<numThread; t++, p+=chunkSize) {
thread[t]=new Thread(new FillInJob(array, t, p, chunkSize));
}

// start the threads
for(Thread t: thread) t.start();
// now all running concurrently

// wait for completion
try {
for(Thread t: thread) t.join();
} catch(InterruptedException ex) {
throw new AssertionError(ex);
}

// use result
System.out.println(java.util.Arrays.toString(array));
}
}
class FillInJob implements Runnable {
private final int[] targetArray;
private final int myID, startIndex, endIndex;

FillInJob(int[] target, int id, int start, int size) {
targetArray=target;
myID=id;
startIndex=start;
endIndex=start+size;
}

public void run() {
for(int ix=startIndex; ix<endIndex; ix++)
targetArray[ix]=myID;
}
}

关于java - 实现同步以获得正确的线程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com