同步对象上的java多线程性能

同步对象上的java多线程性能,java,multithreading,Java,Multithreading,我试图用sync'ed对象测试多线程性能。然而, 对于1个线程或16个线程,执行时间相同 代码的其余部分在这里。 public void run(){ 开始=新日期().getTime(); 系统输出打印项次(开始); 而(threadlist.size()

我试图用sync'ed对象测试多线程性能。然而, 对于1个线程或16个线程,执行时间相同

代码的其余部分在这里。

public void run(){
开始=新日期().getTime();
系统输出打印项次(开始);
而(threadlist.size()<9000){//16或更多
//尝试{Thread.sleep(100);}catch(异常f){}
线程t=新线程(新的可运行线程(){
公开募捐{
而(增加(1,3)<1000000);
end=新日期().getTime();
System.out.println((结束-开始));
}
});
添加(t);
while(threadlist.iterator().hasNext()){
threadlist.iterator().next().start();
请尝试{threadlist.iterator().next().join();}捕获(异常a){}
}
}
}

您的代码存在一些问题。第一:

public void run() {
       while (true) {
            add(1, 3);
        }
 }
这些线程从未停止工作,我建议将您的逻辑改写为:

   public void (run) {
        while(add(1,3) < 1000000);
        System.out.println("now 1000000");
   }

   public int add(int val1, int val2) {
        synchronized (this) {
            this.sum1 += val1;
            this.sum2 += val2;
            return this.sum1 + this.sum2;
            }
        }
    }
你想要

if (threadlist.size()  < 16)
因此,基本上您的
add
方法是按顺序运行的,而不是并行运行的,因为线程将等待synchronized(this),并且只在synchronized语句包装的代码块中调用run on

尝试通过添加
start=newdate().getTime()来测量您的时间在并行区域之前,并且
end=newdate().getTime()之后

您可以简单地使用代码:

public void run() {
    start = new Date().getTime();
    System.out.println(start);
    while (threadlist.size() < 16) {
         Thread t = new Thread(() -> {
              while (add(1,3) < 1);
              System.out.println("now 1000000");
        });
        threadlist.add(t);
    }
    threadlist.forEach(Thread::start);
    
    threadlist.forEach(thr-> {
            try { thr.join();} 
            catch (InterruptedException e) { e.printStackTrace();}
     });
     end = new Date().getTime();
     System.out.println("Time taken : "+(end-start));

     public int add(int val1, int val2) {
            synchronized (this) {
                this.sum1 += val1;
                this.sum2 += val2;
                return this.sum1 + this.sum2;
                }
          }
      }
public void run(){
开始=新日期().getTime();
系统输出打印项次(开始);
while(threadlist.size()<16){
线程t=新线程(()->{
而(增加(1,3)<1);
System.out.println(“现在为1000000”);
});
添加(t);
}
forEach(线程::start);
threadlist.forEach(thr->{
试试{thr.join();}
catch(InterruptedException e){e.printStackTrace();}
});
end=新日期().getTime();
System.out.println(“所用时间:”+(结束-开始));
公共整数相加(整数1,整数2){
已同步(此){
这个.sum1+=val1;
这个.sum2+=val2;
返回this.sum1+this.sum2;
}
}
}

您的代码存在一些问题。第一:

public void run() {
       while (true) {
            add(1, 3);
        }
 }
这些线程从未停止工作,我建议将您的逻辑改写为:

   public void (run) {
        while(add(1,3) < 1000000);
        System.out.println("now 1000000");
   }

   public int add(int val1, int val2) {
        synchronized (this) {
            this.sum1 += val1;
            this.sum2 += val2;
            return this.sum1 + this.sum2;
            }
        }
    }
你想要

if (threadlist.size()  < 16)
因此,基本上您的
add
方法是按顺序运行的,而不是并行运行的,因为线程将等待synchronized(this),并且只在synchronized语句包装的代码块中调用run on

尝试通过添加
start=newdate().getTime()来测量您的时间在并行区域之前,并且
end=newdate().getTime()之后

您可以简单地使用代码:

public void run() {
    start = new Date().getTime();
    System.out.println(start);
    while (threadlist.size() < 16) {
         Thread t = new Thread(() -> {
              while (add(1,3) < 1);
              System.out.println("now 1000000");
        });
        threadlist.add(t);
    }
    threadlist.forEach(Thread::start);
    
    threadlist.forEach(thr-> {
            try { thr.join();} 
            catch (InterruptedException e) { e.printStackTrace();}
     });
     end = new Date().getTime();
     System.out.println("Time taken : "+(end-start));

     public int add(int val1, int val2) {
            synchronized (this) {
                this.sum1 += val1;
                this.sum2 += val2;
                return this.sum1 + this.sum2;
                }
          }
      }
public void run(){
开始=新日期().getTime();
系统输出打印项次(开始);
while(threadlist.size()<16){
线程t=新线程(()->{
而(增加(1,3)<1);
System.out.println(“现在为1000000”);
});
添加(t);
}
forEach(线程::start);
threadlist.forEach(thr->{
试试{thr.join();}
catch(InterruptedException e){e.printStackTrace();}
});
end=新日期().getTime();
System.out.println(“所用时间:”+(结束-开始));
公共整数相加(整数1,整数2){
已同步(此){
这个.sum1+=val1;
这个.sum2+=val2;
返回this.sum1+this.sum2;
}
}
}

自@dreamcrash应答后,您的代码已显著更新

当前版本存在以下问题:

这将启动一个线程,然后立即坐在那里,旋转它的拇指,直到该线程完成其工作,然后将启动另一个线程因此,一次活动线程不超过1个

catch(异常a){}

您正在学习/调试,您是这样做的吗?哦,天哪。不要。永远不要写这样的捕捉块。更新IDE或肌肉内存:正确的“我现在不想考虑异常”代码是
catch(异常a){throw new RuntimeException(“Unhandled”,a);}
。要明确的是,这不是问题所在,但这是一个非常坏的习惯,需要唤醒它

synchronized(this){

我真的怀疑你是否解决了我前面提到的“加入”问题。这会运行得更快。这个同步调用很重要,但它也会造成太多的阻塞,因此你可能在这里看不到任何实际好处

更一般地说,您试图加速的计算涉及累加器

累加器是“并行在这里是完全不可能的,它是无望的”的另一个词

如果要并行化算法,则该算法不能涉及累加器,这是多线程(至少,如果多线程的目的是加快速度)正在做的事情。使用线程无法使该算法更快。句号


通常,算法可以被重写以停止依赖累加器。但这显然是一个观察效果的练习,因此,只需找到其他任何东西,真的。在整个计算过程中,不要锁定单个对象:只有一个线程在实际工作,其他999个线程都在等待。

自那以后,您的代码得到了显著的更新”梦坠机回答

当前版本存在以下问题:

这将启动一个线程,然后立即坐在那里,旋转它的大拇指,直到该线程完成其工作,然后启动另一个线程。因此,您永远不会再