Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程并发-使用字符串对象';s同步锁_Java_Multithreading_Oop_Concurrency - Fatal编程技术网

Java 线程并发-使用字符串对象';s同步锁

Java 线程并发-使用字符串对象';s同步锁,java,multithreading,oop,concurrency,Java,Multithreading,Oop,Concurrency,上述测试产生以下输出 class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1 = new Thread(rt); Thread t2 = new Thread(rt); Thread t3 = new Thread(rt); t1.start

上述测试产生以下输出

class ZiggyTest{

    public static void main(String[] args){

        RunnableThread rt = new RunnableThread();

        Thread t1 = new Thread(rt);
        Thread t2 = new Thread(rt);
        Thread t3 = new Thread(rt);

        t1.start();
        t2.start();
        t3.setPriority(10);
        t3.start();

        try{
            t3.join();
            t2.join();
            t1.join();

            System.out.println("Joined");
        }catch(InterruptedException e){System.out.println(e);}


        Thread t4 = new Thread(new RunnableThread());
        Thread t5 = new Thread(new RunnableThread());
        Thread t6 = new Thread(new RunnableThread());

        t4.start();
        t5.start();
        t6.start();
    }

}
我不明白为什么最后三个线程没有像前三个线程那样使用String对象作为共享锁。即使最后三个线程使用的是不同的“RunnableThread”实例,它们是否应该同步,因为字符串常量池中只有一个“str”副本

谢谢

编辑 哎呀。。我忘了包括RunnableThread

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-2
Thread-2
Thread-2
Thread-2
Thread-2
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Joined
Thread-3
Thread-3
Thread-4
Thread-4
Thread-3
Thread-4
Thread-5
Thread-4
Thread-3
Thread-4
Thread-5
Thread-3
Thread-5
Thread-5
Thread-5

你的线程在任何
字符串
对象上都不同步,更不用说在同一个对象上了,所以我不确定这个想法是从哪里来的。由于此行中的
synchronized
关键字,它们在
RunnableThread
对象本身上同步:

class RunnableThread implements Runnable{

    String str = "HELLO";

    public void run(){
        runMe();
    }

    public synchronized void runMe(){
        for (int a : new int[]{1,2,3,4,5}){
            System.out.println(Thread.currentThread().getName());
        }
    }
}
为最后三个线程使用单独的
RunnableThread
对象意味着它们独立运行

现在,如果您确实想锁定这个全局
字符串
对象,那么该方法如下所示

public synchronized void runMe(){

最好将
str
设为final

您的线程在任何
字符串
对象上都不同步,更不用说相同的对象了,所以我不确定这个想法来自何方。由于此行中的
synchronized
关键字,它们在
RunnableThread
对象本身上同步:

class RunnableThread implements Runnable{

    String str = "HELLO";

    public void run(){
        runMe();
    }

    public synchronized void runMe(){
        for (int a : new int[]{1,2,3,4,5}){
            System.out.println(Thread.currentThread().getName());
        }
    }
}
为最后三个线程使用单独的
RunnableThread
对象意味着它们独立运行

现在,如果您确实想锁定这个全局
字符串
对象,那么该方法如下所示

public synchronized void runMe(){

最好将
str
final

作为
RunnableThread
的源代码,这样我们才能回答这个问题!锁定字符串?你为什么要这么做?我在现实生活中不会这么做,但这是一个可以在现实生活认证考试中出现的问题:)为什么这个问题被否决了?(挠头…)我们需要
RunnableThread
的源代码来回答这个问题!锁定字符串?你为什么要这么做?我在现实生活中不会这么做,但这是一个可以在现实生活认证考试中出现的问题:)为什么这个问题被否决了?(挠头)是的,看起来这就是我犯的错误。我修改了它以在str上同步,现在它正按预期工作。谢谢。使用字符串文字或缓存数字通常被认为是个坏主意。如果您想使用共享锁,可以使用
synchronized(getClass())
Yes。看起来这是我犯的错误。我修改了它以在str上同步,现在它正按预期工作。谢谢。使用字符串文字或缓存数字通常被认为是个坏主意。如果要使用共享锁,可以使用
synchronized(getClass())