gpt4 book ai didi

Thread.currentThread in case of parallel threads(在使用并行线程的情况下,使用Thread.CurrentThread)

转载 作者:bug小助手 更新时间:2023-10-25 17:48:22 26 4
gpt4 key购买 nike



Current implementation of Thread.sleep is:

当前Thad.sleep的实现是:


public static void sleep(long millis) throws InterruptedException {
if (millis < 0L) {
throw new IllegalArgumentException("timeout value is negative");
} else {
Thread var3 = currentThread();
.....

currentThread is native method.
In a multi-core system we can have multiple threads actually running at same time.

currentThread是native方法。在多核系统中,我们可以同时运行多个线程。


How is current thread then decided?

那么当前的主题是如何决定的呢?


更多回答

Threads run instructions and are mapped to cores. One thread is associated with at most one core at any one point in time. Therefore, each instruction is associated with exactly on e thread. Each instruction is executed on exactly one core, therefore bound to exactly one thread. Thread.currentThread() is the thread that executes the current statement (or thread of executions, to be precise).

线程运行指令并映射到核心。在任何时间点,一个线程最多与一个核相关联。因此,每条指令恰好与e线程上的指令相关联。每条指令只在一个核心上执行,因此只绑定到一个线程。CurrentThread()是执行当前语句的线程(准确地说,是执行的线程)。

it is just the same Thread that is calling the sleep method (this is not necessarily related (directly) to any core)

调用睡眠方法的线程就是同一个线程(这不一定(直接)与任何内核相关)

The call to method currentThread() is executed on a specific thread, that thread is identified through native code and returned.

对currentThread()方法的调用在特定线程上执行,该线程通过本机代码标识并返回。

You might want to look at how threading is implemented for a given OS (e.g. libc in Linux).

您可能想看看线程是如何针对给定的操作系统实现的(例如,Linux中的libc)。

优秀答案推荐

"Current thread" does not mean what you think it means. What it means is, the thread that called the Thread.currentThread() function. The name is left over from ancient times when computers only had one CPU each. In those days, only one thread could be "current," and so the thread that called the function and the current thread were one and the same.

“当前帖子”并不意味着你认为它意味着什么。它的意思是,调用Thread.CurrentThread()函数的线程。这个名字是从古代遗留下来的,当时计算机每台只有一个CPU。在那些日子里,只有一个线程可以是“当前”的,因此调用函数的线程和当前线程是同一个线程。


更多回答

"The name is left over from ancient times when computers only had one CPU each." - I find the name currentThread to be perfectly applicable to the multicore too. E.g. it is the "thread which currently executes our code". It describes the multithreading aspect, not a multicore one.

“这个名字是从古代遗留下来的,当时计算机每台电脑只有一个CPU。”-我发现CurrentThread这个名字也非常适用于多核。例如,它是“当前执行我们代码的线程”。它描述了多线程方面,而不是多核方面。

@Tsyvarev, Sure, but which thread is "current" when multiple threads currently are executing our code? Depends what you think "current" means. A name like Thread.self() would have caused less confusion. But the original authors of the Java language were some pretty smart dudes. If they didn't think of it at the time, then I can't pretend that I would have thought of it back then either.

@Tsyvarev,当然,但是当多个线程当前正在执行我们的代码时,哪个线程是“当前”的?这取决于你认为“当前”是什么意思。像Thread.self()这样的名称会引起较少的混淆。但Java语言的原始作者是一些相当聪明的家伙。如果他们当时没有想到,那么我也不能假装我当时也会想到它。

P.S., where the name really comes from: In a single-CPU OS, there would have been a variable in the kernel code that pointed to the context record for the thread that currently was being executed by the CPU. The variable would have a name like current_thread, and the system call that a user-mode function could call to find out the identity of the thread that was executing it was pretty much just a "getter" for that kernel variable.

另外,这个名称的真正来源:在单CPU操作系统中,内核代码中会有一个变量指向当前由CPU执行的线程的上下文记录。该变量的名称类似CURRENT_THREAD,用户模式函数可以调用系统调用来确定执行该变量的线程的身份,该系统调用几乎就是该内核变量的“getter”。

"A name like Thread.self() would have caused less confusion." - Assuming a Thread is a threading library, and not a representation of the specific thread, I find the method .self even more confusing (as pointing to the library, not to the thread). Actually, I don't think that there is a name which would represent a conception of the "current thread" in non-confusing way. But "Current Thread" is de-facto a standard name for the concept, and it is used in many programming languages.

“像Thread.self()这样的名称会引起更少的混淆。”-假设Thread是一个线程库,而不是特定线程的表示形式,我发现方法.self更加令人困惑(因为指向的是库,而不是线程)。事实上,我不认为有一个名称能够以一种不令人混淆的方式来代表“当前线程”的概念。但“当前线程”实际上是这个概念的标准名称,它在许多编程语言中都有使用。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com