如何从信号量中获取获取的索引
how to get the acquired index from Semaphore
我有一个大小为 4 的 int 数组,一次只有一个线程可以访问一个数组单元格。
我考虑过使用信号量,但我不知道如何或是否有办法获取获取的索引
我构建了一个代码示例来解释黄油:
public class Temp {
private ExecutorService executeService;
private Semaphore semaphore;
private int[] syncArray; // only one thread can access an array cell at the same time
public Temp() {
syncArray = new int[]{1,2,3,4};
executeService = Executors.newFixedThreadPool(10);
semaphore = new Semaphore(syncArray.length, true);
for(int i = 0;i < 100; i++) {
executeService.submit(new Runnable() {
@Override
public void run() {
semaphore.acquire();
// here I want to access one of the array cell
// dose not matter witch one as long as no other thread is currently use it
int syncArrayIndex = semaphore.getAcquiredIndex(); // is something like this possible?
syncArray[syncArrayIndex] += ...;
semaphore.release();
}
});
}
}
}
编辑:
这是一段看起来更接近我真正问题的代码:
public class Temp {
private ExecutorService executeService;
private Semaphore semaphore;
private static ChromeDriver driver;
public Temp() {
executeService = Executors.newFixedThreadPool(10);
}
public Future<WikiPage> getWikiPage(String url) {
executeService.submit(new PageRequest(url) {
});
}
private static class PageRequest implements Callable<WikiPage> {
String url;
public PageRequest(String url) {
this.url = url;
}
@Override
public WikiPage call() throws Exception {
String html = "";
synchronized (driver) {
html = ...// get the wiki page, this part takes a log time
};
WikiPage ret = ...// parse the data to the WikiPage class
// this part takes less time but depend on the sync block above
return ret;
}
}
}
@Kayaman 我不确定我理解你的评论,问题是我 return 一个未来。您对如何将我的代码改进到 运行 更快有什么建议吗?
不,信号量在这里没有用。它只知道它有多少个许可,信号量中没有"indices"。
您可以改用 AtomicIntegerArray,但如果您解释了根本问题,可能会有更适合使用的 class。
我有一个大小为 4 的 int 数组,一次只有一个线程可以访问一个数组单元格。 我考虑过使用信号量,但我不知道如何或是否有办法获取获取的索引 我构建了一个代码示例来解释黄油:
public class Temp {
private ExecutorService executeService;
private Semaphore semaphore;
private int[] syncArray; // only one thread can access an array cell at the same time
public Temp() {
syncArray = new int[]{1,2,3,4};
executeService = Executors.newFixedThreadPool(10);
semaphore = new Semaphore(syncArray.length, true);
for(int i = 0;i < 100; i++) {
executeService.submit(new Runnable() {
@Override
public void run() {
semaphore.acquire();
// here I want to access one of the array cell
// dose not matter witch one as long as no other thread is currently use it
int syncArrayIndex = semaphore.getAcquiredIndex(); // is something like this possible?
syncArray[syncArrayIndex] += ...;
semaphore.release();
}
});
}
}
}
编辑: 这是一段看起来更接近我真正问题的代码:
public class Temp {
private ExecutorService executeService;
private Semaphore semaphore;
private static ChromeDriver driver;
public Temp() {
executeService = Executors.newFixedThreadPool(10);
}
public Future<WikiPage> getWikiPage(String url) {
executeService.submit(new PageRequest(url) {
});
}
private static class PageRequest implements Callable<WikiPage> {
String url;
public PageRequest(String url) {
this.url = url;
}
@Override
public WikiPage call() throws Exception {
String html = "";
synchronized (driver) {
html = ...// get the wiki page, this part takes a log time
};
WikiPage ret = ...// parse the data to the WikiPage class
// this part takes less time but depend on the sync block above
return ret;
}
}
}
@Kayaman 我不确定我理解你的评论,问题是我 return 一个未来。您对如何将我的代码改进到 运行 更快有什么建议吗?
不,信号量在这里没有用。它只知道它有多少个许可,信号量中没有"indices"。
您可以改用 AtomicIntegerArray,但如果您解释了根本问题,可能会有更适合使用的 class。