java中多线程环境中的静态方法行为 [英] Static method behavior in multi-threaded environment in java

查看:131
本文介绍了java中多线程环境中的静态方法行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的愚蠢问题困扰着我并在脑海中提出了几个论点。
我想抛出以下问题的所有疑问。

There's a simple stupid question that bother me and make several arguments in my mind. I want to throw out all the doubts about below questions.

class Clstest{

    public static String testStaticMethod(String inFileStr) {

        // section 0

        // section 1

        // do something with inFileStr

        // section 2

        // section 3

        return inFileStr;

    }

}

我们假设在那里是五个线程同时执行对 Clstest.testStaticMethod(arg-n)的调用。

Let's assume there are five threads are each executing a call to Clstest.testStaticMethod("arg-n") at the same time.

线程1调用 Clstest.testStaticMethod(arg-1)

当线程1在第1节,第2个线程调用 Clstest.testStaticMethod(arg-2)

When thread 1 is in the section 1, thread 2 calls Clstest.testStaticMethod("arg-2").

然后会发生什么线程1?它会进入睡眠状态吗?

Then what will happen to Thread 1? Will it go to sleep state?

当线程1获得机会时,它会从暂停的第1部分恢复执行吗?

When Thread 1 got the chance will it resume the execution from section 1 where it was paused?

如果有一个 Clstest.testStaticMethod 并且所有 Clstest.testStaticMethod 在所有五个线程?

How it happens when there's one Clstest.testStaticMethod and same Clstest.testStaticMethod is shared between all five threads?

是否有可能交换多个线程发送的 inFileStr

Is there any possibility to interchange the inFileStr sent by multiple threads?

推荐答案

Hans Passant的答案很好。但我想我会尝试在一个稍微简单的层面上解释一下,遇到这个并且对Java来说很新的人。这里是..

Hans Passant's answer is good. But I thought I would try and explain at a slightly more simple level for anybody who comes across this and is newish to Java. Here goes..

java中的内存分为两类 - 堆和堆栈。堆是所有对象所在的位置,堆栈是线程执行工作的位置。每个线程都有自己的堆栈,无法访问其他堆栈。每个线程还有一个指向代码的指针,指向它们当前正在运行的代码位。

Memory in java is split up into two kinds - the heap and the stacks. The heap is where all the objects live and the stacks are where the threads do their work. Each thread has its own stack and can't access each others stacks. Each thread also has a pointer into the code which points to the bit of code they're currently running.

当一个线程开始运行一个新方法时,它会保存参数和该方法中的局部变量在其自己的堆栈上。其中一些值可能是指向堆上对象的指针。如果两个线程同时运行相同的方法,则它们的代码指针都指向该方法,并在其堆栈上拥有自己的参数和局部变量副本。如果堆栈上的东西指向堆上的相同对象,它们只会相互干扰。在这种情况下,可能会发生各种各样的事情。但正如汉斯指出的那样,字符串是不可变的(不能改变)所以如果这是唯一被共享的对象,我们就是安全的。

When a thread starts running a new method it saves the arguments and local variables in that method on its own stack. Some of these values might be pointers to objects on the heap. If two threads are running the same method at the same time they will both have their code pointers pointing at that method and have their own copies of arguments and local variables on their stacks. They will only interfere with each other if the things on their stacks point to the same objects on the heap. In which case all sorts of things might happen. But as Hans points out, Strings are immutable (cannot be changed) so we're safe if this is the only object being "shared".

这么多线程可以是运行相同的方法。它们可能不会同时运行 - 它取决于您的机器上有多少核心,因为JVM将Java线程映射到OS线程,这些线程被安排到硬件线程上。因此,在没有使用复杂的同步机制的情况下,您几乎无法控制这些线程交错的方式。

So many threads can be running the same method. They might not be running at the same time - it depends how many cores you have on your machine as the JVM maps Java threads to OS threads, which are scheduled onto hardware threads. You therefore have little control over the way these threads interleave without using complex synchronisation mechanisms.

请注意,休眠是线程对自身的作用。

Note that sleeping is something a thread does to itself.

这篇关于java中多线程环境中的静态方法行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆