简体   繁体   English

Java线程行为

[英]Java Threading behavior

I saw the following example on the internet: 我在互联网上看到以下示例:

public class TwoThreads {
public static class Thread1 extends Thread {
    public void run() {
        System.out.println("A");
        System.out.println("B");
    }
}
public static class Thread2 extends Thread {
    public void run() {
        System.out.println("1");
        System.out.println("2");
    }
}
public static void main(String[] args) {
    new Thread1().start();
    new Thread2().start();
}

} }


My question is : 我的问题是:

  1. It is guarantee that "A" will be printed Before "B" and "1" will be printed before "2", but is it possible that "1" will be printed twice successively by another thread?.In this piece of code we have at least 3 threads(1 main and 2 created). 可以保证在“ B”之前打印“ A”,在“ 2”之前打印“ 1”,但是是否有可能通过另一个线程将“ 1”连续打印两次?至少有3个线程(创建了1个主线程和2个线程)。 can we imagine the scheduler runs 1 thread: new Thread1().start(); 我们可以想象调度程序运行1个线程吗:new Thread1()。start(); then gave up immediately after System.out.println("1"); 然后在System.out.println(“ 1”)之后立即放弃; then again run another threat in Thread1().start(); 然后再次在Thread1()。start()中运行另一个威胁; that prints "1" again ? 再次打印“ 1”?

  2. I am using NetBeans IDE, it seems running such a program always lead to the same first result, so it seems there something with caching. 我正在使用NetBeans IDE,似乎运行这样的程序总是会导致相同的第一个结果,因此似乎存在缓存。 From my understanding you deal with that with declaring volatile variables, can it be done here,how ? 根据我的理解,您通过声明volatile变量来解决这个问题,请问如何在这里完成? if not then what is the solution for caching ? 如果不是,那么缓存的解决方案是什么?

  3. In today's Computer's processor, we mostly have 2 processors,and still we find many multi-threading programs on the net uses more than 2 threads! 在当今计算机的处理器中,我们大多数情况下有2个处理器,但仍然发现网络上有许多使用2个以上线程的多线程程序! isn't this process becomes heavy and slow regarding compiling ? 编译的过程难道不是很繁琐吗?

1) There is no guarantee in what order the threads will proceed. 1)无法保证线程以什么顺序进行。

2) The order is not randomized, either, though. 2)顺序也不是随机的。 So if you run the program under identical (or very similar) conditions, it will probably yield the same thread interleaving. 因此,如果您在相同(或非常相似)的条件下运行程序,则可能会产生相同的线程交织。 If you need to have a certain behaviour (including randomized behaviour) you need to synchronized things yourself. 如果您需要某种行为(包括随机行为),则需要自己同步。

3) A CPU with two cores can only run two threads at the same time, but most threads spend most of their time not actually using the CPU, but waiting for something like I/O or user interaction. 3)具有两个内核的CPU只能同时运行两个线程,但是大多数线程大部分时间不是在实际使用CPU,而是在等待诸如I / O或用户交互之类的时间。 So you can gain a lot from having more than two threads (only two can concurrently compute, but hundreds can concurrently wait). 因此,拥有两个以上的线程可以使您受益匪浅(只有两个线程可以并行计算,而数百个线程可以同时等待)。 Take a look at node.js, a recently popular alternative to multi-threaded programming that achieves great throughput for concurrent requests while having only a single thread of execution. 请看一下node.js,它是最近流行的多线程编程替代方案,它在仅执行一个线程的情况下,就可以为并发请求实现高吞吐量。

Answer to your 1/2 question: Though threads run parallel code inside run method of thread is always executed sequentially. 回答您的1/2问题:尽管线程在线程的run方法内部运行并行代码,但始终按顺序执行。

Answer to your 3 question you can best tune your. 回答您的3个问题,您可以最好地调整您的问题。 Application if number of processors = number of threads but this is not a complete truth since if thread is waiting for some blocking operation then it will lead to un optimized performance since during that time another thread could run. 如果处理器数量=线程数量,那么应用程序不是一个完整的事实,因为如果线程正在等待某些阻塞操作,那么它将导致未优化的性能,因为在此期间可能会运行另一个线程。

  1. No. You are not synchronizing your threads in any way, so the exact execution order will be at the mercy of the scheduler. 不。您没有以任何方式同步线程,因此确切的执行顺序将由调度程序控制。 Given how your threads are implemented, I don't see how you could ever having "1" (or "A") being printed twice by a single thread. 鉴于线程的实现方式,我看不到如何通过单个线程将“ 1”(或“ A”)打印两次。

  2. What caching? 什么缓存? And what variables? 什么变量? Your example code has no variables, and therefore nothing that would be appropriate to use with the volatile keyword. 您的示例代码没有变量,因此没有什么适合与volatile关键字一起使用。 It's quite likely that on a given machine running this program will always produce the same result. 在运行该程序的给定计算机上,很有可能总是产生相同的结果。 As noted in #1, you're at the mercy of the scheduler. 如#1所述,您受调度程序的支配。 If the scheduler always behaves the same way, you'll always get the same result. 如果调度程序始终以相同的方式运行,那么您将始终获得相同的结果。 Caching has nothing to do with it. 缓存与它无关。

  3. That depends upon what the threads are doing. 这取决于线程在做什么。 If every thread has enough work to load one CPU core to 100%, then yes, having more threads than you have CPU cores is pointless. 如果每个线程都有足够的工作量来将一个CPU内核加载到100%,那么是的,拥有比CPU内核更多的线程是没有意义的。 However, this is very rarely the case. 但是,这种情况很少发生。 Many threads will spend most of their time sleeping, or waiting for I/O to complete, or otherwise doing things that are not demanding enough to fully load a CPU core. 许多线程将把大部分时间都花在睡觉,等待I / O完成或以其他方式完成不足以完全加载CPU内核的事情上。 In such a case there's no problem whatsoever with having more threads that CPU cores. 在这种情况下,拥有更多的CPU核心线程是没有问题的。 In fact, multithreading predates mainstream multicore CPU's, and even back in the days when none of us had more than one CPU core it was still extremely beneficial to be able to have more than one thread. 实际上,多线程早于主流多核CPU,甚至在我们每个人都没有一个以上CPU内核的时代,拥有多个线程仍然非常有益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM