java中的synchronized无法同步

huangapple go评论59阅读模式
英文:

java synchronized fails to synchronize

问题

如何同步运行方法?
如果我在这里使用synchronized关键字,它不起作用。每次都会给我不同的输出?

class MyClass implements Runnable 
{
    boolean flag;
    public MyClass(boolean val)
    {
       flag=val;
    }
    public synchronized void run()  
    {
        long id=Thread.currentThread().getId();
        int start=(flag)?1:2;
        for(int i=start;i<=10;i+=2)
        {
           System.out.println("线程 "+id+" 输出:"+i);
        }
    }
}
public class Main {
    public static void main(String[] args)
    {
        Thread t1=new Thread(new MyClass(false));
        t1.start();
        Thread t2=new Thread(new MyClass(true));
        t2.start();
    }
}
英文:

How do I synchronize the run method?
If I use the synchronized keyword here, it doesn't work. It gives me a different output every time?

class MyClass implements Runnable 
{
    boolean flag;
    public MyClass(boolean val)
    {
       flag=val;
    }
    public synchronized void run()  
    {
        long id=Thread.currentThread().getId();
        int start=(flag)?1:2;
        for(int i=start;i&lt;=10;i+=2)
        {
           System.out.println(&quot;Thead &quot;+id+&quot; prints:&quot;+i);
        }
    }
}
public class Main {
    public static void main(String[] args)
    {
        Thread t1=new Thread(new MyClass(false));
        t1.start();
        Thread t2=new Thread(new MyClass(true));
        t2.start();
    }
}

答案1

得分: 2

每个线程都在使用不同的MyClass实例。如果在非静态实例上使用synchronized,这确保了该方法不会在同一MyClass实例的情况下被多个线程同时执行。如果要对所有MyClass实例进行同步,该方法必须是静态的。

class MyClass implements Runnable {

    boolean flag;

    public MyClass(boolean val)
    {
       flag=val;
    }  

    private static synchronized void runTask(boolean flag) {
        long id=Thread.currentThread().getId();
        int start=(flag)?1:2;
        for(int i=start;i<=10;i+=2)
        {
            System.out.println("线程 "+id+" 输出:"+i);
        }
    }

    public void run()  
    {
        runTask(flag);
    }

}
英文:

Each thread is using a different instance of MyClass. If you use synchronized on a non static instance, this ensure the method is not executed by several threads at the same for the same MyClass instance. If you want to synchronize for all instances of MyClass, the method has to be static.

class MyClass implements Runnable {
    
    boolean flag;

    public MyClass(boolean val)
    {
       flag=val;
    }  

    private static synchronized void runTask(boolean flag) {
        long id=Thread.currentThread().getId();
        int start=(flag)?1:2;
        for(int i=start;i&lt;=10;i+=2)
        {
            System.out.println(&quot;Thead &quot;+id+&quot; prints:&quot;+i);
        }
    }

    public void run()  
    {
        runTask(flag);
    }

}

huangapple
  • 本文由 发表于 2020年9月25日 14:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64058810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定