Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 如何正确地从多线程类获取静态变量?_Java_Multithreading - Fatal编程技术网

Java 如何正确地从多线程类获取静态变量?

Java 如何正确地从多线程类获取静态变量?,java,multithreading,Java,Multithreading,假设我有这样的代码: 我希望输出如下所示: 但结果是: 我在这里该怎么办?谢谢你的帮助。Q.对于这种情况,您应该使用而不是int,并更改方法 public class Test { public static void main(String[] args) throws InterruptedException { final A thread1 = new A(); final A thread2 = new A();

假设我有这样的代码:



我希望输出如下所示:



但结果是:




我在这里该怎么办?谢谢你的帮助。Q.

对于这种情况,您应该使用而不是int,并更改方法

public class Test
{

    public static void main(String[] args) throws InterruptedException
    {
        final A thread1 = new A();
        final A thread2 = new A();
        try
        {
            thread1.join();
            thread2.join();
        }
        catch (final InterruptedException e)
        {
            e.printStackTrace();
        }
        Thread.sleep(1000);
        System.out.println("Main thread ends here");
    }

    public static class A extends Thread
    {
        private static final AtomicInteger id = new AtomicInteger(0);

        A()
        {
            start();
        }

        private synchronized int getID()
        {
            return id.get();
        }

        @Override
        public synchronized void run()
        {
            id.incrementAndGet();
            System.out.println(getID());
        }
    }
}

对于这种情况,应该使用而不是int,并更改方法

public class Test
{

    public static void main(String[] args) throws InterruptedException
    {
        final A thread1 = new A();
        final A thread2 = new A();
        try
        {
            thread1.join();
            thread2.join();
        }
        catch (final InterruptedException e)
        {
            e.printStackTrace();
        }
        Thread.sleep(1000);
        System.out.println("Main thread ends here");
    }

    public static class A extends Thread
    {
        private static final AtomicInteger id = new AtomicInteger(0);

        A()
        {
            start();
        }

        private synchronized int getID()
        {
            return id.get();
        }

        @Override
        public synchronized void run()
        {
            id.incrementAndGet();
            System.out.println(getID());
        }
    }
}


private静态volatile int id=0
volatile
是不够的,
++
不是原子的:->使用
AtomicInteger
。为什么
线程
中有一个
线程
?此外,决不要
扩展线程
——这会产生各种奇怪的行为,请使用
可运行的
。您从不启动
A
实例;启动一个内部
线程,该线程不执行任何操作。在ctor中增加变量,而不是在
run()
方法中。这段代码几乎在所有方面都是错误的——请从阅读Java中线程的基本教程开始。谢谢,我必须解决它。这个代码没有编译。方法getId在类线程中定义,它返回一个长的而不是int。
private static volatile int id=0
volatile
是不够的,
++
不是原子的:->使用
AtomicInteger
。为什么
线程
中有一个
线程
?此外,决不要
扩展线程
——这会产生各种奇怪的行为,请使用
可运行的
。您从不启动
A
实例;启动一个内部
线程,该线程不执行任何操作。在ctor中增加变量,而不是在
run()
方法中。这段代码几乎在所有方面都是错误的——请从阅读Java中线程的基本教程开始。谢谢,我必须解决它。这个代码没有编译。方法getId是在类Thread中定义的,它返回一个long,而不是int。除非您修复了线程混乱的情况,否则这实际上不会改变任何东西。原始输出始终为
12
,因为增量在ctor中同步发生。(好吧,最初的输出应该是,现在没有输出,因为
run
从未执行过)更仔细地阅读代码-
id
仅由主线程进行变异。代码没有做任何有意义的事情-添加
AtomicInteger
不会修复OP编写的废话。我修复了答案以打印正确numbers@luk2302我修改了OP类,如果你打开question@Spara你的门锁坏了,OP已经同意这个问题是重复的-他想要解决的真正问题是原子增量,重复的答案是。除非你修复混乱的线程情况,否则这不会真正改变任何事情。原始输出始终为
12
,因为增量在ctor中同步发生。(好吧,最初的输出应该是,现在没有输出,因为
run
从未执行过)更仔细地阅读代码-
id
仅由主线程进行变异。代码没有做任何有意义的事情-添加
AtomicInteger
不会修复OP编写的废话。我修复了答案以打印正确numbers@luk2302我修改了OP类,如果你打开question@Spara你的门锁坏了,OP已经同意这个问题是重复的——他想要解决的真正问题是原子增量的问题,重复的答案是。
2
2
public class Test
{

    public static void main(String[] args) throws InterruptedException
    {
        final A thread1 = new A();
        final A thread2 = new A();
        try
        {
            thread1.join();
            thread2.join();
        }
        catch (final InterruptedException e)
        {
            e.printStackTrace();
        }
        Thread.sleep(1000);
        System.out.println("Main thread ends here");
    }

    public static class A extends Thread
    {
        private static final AtomicInteger id = new AtomicInteger(0);

        A()
        {
            start();
        }

        private synchronized int getID()
        {
            return id.get();
        }

        @Override
        public synchronized void run()
        {
            id.incrementAndGet();
            System.out.println(getID());
        }
    }
}