什么是用于控制Java中对集合的多次访问的最慢的线程安全机制?
我将对象添加到集合的顶部,我不确定什么是最佳表现集合.它会是矢量还是队列?我原本以为ArrayList会很快,但我做了一些实验而且速度非常慢.
编辑:在我的插入测试中,使用volatile的矢量delared似乎是最快的?
如果我使用并发hashmap并且我有设置和获取值的方法,因为我使用并发hashmap我需要使getter和setter同步吗?这是多余的吗?一种设计更好吗?
另外,没有同步的并发hashmap比具有synchronized getter和setter的hashmap更快吗?这是一个高性能系统
谢谢
我已经使用其他成员给我的想法然后更改了几个容器来汇总下面的代码.对于我的生活,我真的无法理解其中的一些.代码的原因是我希望将函数作为参数传递.我特别不理解的代码部分是:
doFunc(numbers, new IFunction() {
public void execute(Object o) {
Integer anInt = (Integer) o;
anInt++;
System.out.println(anInt);
}
});
Run Code Online (Sandbox Code Playgroud)
我在某种程度上理解我们正在使用一个接口来表示一个使用对象的函数(我认为?).这是完整的代码:
public static void main(String[] args) {
Integer[] strArray = new Integer[]{1,2,3,4,5};
List numbers = Arrays.asList(strArray);
doFunc(numbers, new IFunction() {
public void execute(Object o) {
Integer anInt = (Integer) o;
anInt++;
System.out.println(anInt);
}
});
for(int y =0; y<numbers.size();y++){
System.out.println(numbers.get(y));
}
}
public static void doFunc(List c, IFunction f) {
for (Object o : c) {
f.execute(o);
}
}
public interface IFunction …Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来为加密方法找到某种纳秒级时序.我在stackoverflow上找到了这个代码,它似乎没有在VS2010中编译,但我无法弄清楚为什么.错误是'time = GetCpuClocks()'并说"错误C3861:'GetCpuClocks':标识符未找到",我不明白为什么?我在struct声明中也遇到了'int32'类型的问题.
(我假设'时间'声称'长'可以吗?或者它应该是__int64?
提前致谢
int _tmain(int argc, _TCHAR* argv[])
{
__int64 time;
time = GetCpuClocks();
}
inline __int64 GetCpuClocks() {
// Counter
struct { int32 low, high; } counter;
// Use RDTSC instruction to get clocks count
__asm push EAX
__asm push EDX
__asm __emit 0fh
__asm __emit 031h
// RDTSC
__asm mov counter.low, EAX
__asm mov counter.high, EDX
__asm pop EDX
__asm pop EAX
// Return result
return *(__int64 *)(&counter);
}
Run Code Online (Sandbox Code Playgroud) 我可以将while循环与wait()和notify()/ notifyall()一起使用吗?
编辑:我希望waitfornextprice方法的else条件只对putPrice方法执行.
public synchronized void putPrice(String s,int i) {
if (getPrice(s) != i){
this.Prices.put(s, i);
notify();
}
}
public synchronized int waitForNextPrice(String s) {
int b = null;
if (hasPriceChanged(s)==true){
b = getPrice(s);
}
else{
//Need to block here, until putPrice() is called again
while(hasPriceChanged(s)==false) wait();
//Once putPrice has been called, b = getPrice();
b = getPrice(s);
}
return b;
}
Run Code Online (Sandbox Code Playgroud)