简体   繁体   English

每个类默认都有自己的线程吗?

[英]Does each class have its own Thread by default?

I'm working with a GUI and I'm using 我正在使用GUI,正在使用

Thread.sleep();

in some of the classes, and I'm wondering if I need to create a separate Thread for my Main class (the GUI class), or if each class has a separate Thread by default. 在某些类中,我想知道是否需要为我的Main类(GUI类)创建一个单独的Thread,或者默认情况下每个类是否具有一个单独的Thread。 Note, the reason that I bring up Thread.sleep(); 注意,我调出Thread.sleep();的原因是 in the first place is that when working with GUI's Thread.sleep(); 首先是在使用GUI的Thread.sleep(); pretty much freezes your GUI. 几乎冻结了您的GUI。 Anyways my main question is do I need to create a separate Thread for my Main class or if each class has a separate Thread by default. 无论如何,我的主要问题是我是否需要为我的Main类创建一个单独的线程,或者默认情况下每个类是否具有单独的线程。

Thread.sleep() is a static method of Thread class. Thread.sleep()是Thread类的静态方法。 Hence, whichever class you place in a method, during runtime, if a thread call flow encounters this class method where Thread.sleep() in invoked, the thread will be blocked for that amount of time. 因此,在运行时,无论您放置在方法中的哪个类,如果线程调用流遇到此类方法,其中调用Thread.sleep()的时间都会被阻塞。

Your Question: 你的问题:
Anyways my main question is do I need to create a separate Thread for my Main class or if each class has a separate Thread by default. 无论如何,我的主要问题是我是否需要为我的Main类创建一个单独的线程,或者默认情况下每个类是否具有单独的线程。

  • each class has a separate Thread by default -- INCORRECT Statement 每个类默认都有一个单独的线程-INCORRECT语句
    -- Thread class is not inherited by every class -并非每个类都继承线程类
    -- In normal sense, Thread is a call flow. -从通常意义上讲,线程是一个调用流程。 It executes whichever class it encounters thru its method calls. 它执行通过其方法调用遇到的任何类。
    -- Class and Thread are 2 separate concepts. -类和线程是2个独立的概念。
    ---- Class is a definition of an entity,it cannot run by itself, it can be loaded, instantiated with data, method calls can be done and garbage collected. ----类是一个实体的定义,它不能单独运行,可以被加载,用数据实例化,可以进行方法调用和垃圾回收。
    ---- Thread is an execution entity at run-time.It can be started, runnable, blocked, stopped. ----线程是运行时的执行实体,可以启动,运行,阻塞,停止。 To support this concept, Java has provided Thread class or Runnable interface to extend/implement respectively. 为了支持这个概念,Java提供了Thread类或Runnable接口来分别扩展/实现。

  • do I need to create a separate Thread for my Main class? 我需要为我的主类创建一个单独的线程吗?
    -- your mainclass will be executed in a MainThread which is instantiated and started by your JVM. -您的主类将在MainThread中执行,该主线程由JVM实例化并启动。
    -- It is better to define a seperate user-defined Thread to start your GUI. -最好定义一个单独的用户定义线程来启动GUI。
    -- If either in the MainThread (or) user-defined Thread, if it encounters Thread.sleep() during call flow, that particular thread will be blocked. -如果在MainThread(或)用户定义的线程中,如果在调用流程中遇到Thread.sleep(),则该特定线程将被阻止。

One more thing, your question is not clear on your need for usage of Thread.sleep(). 还有一件事,您对使用Thread.sleep()的需求尚不清楚。 You just have given a reason of the resultant usage of it but not the need for usage. 您只是给出了其最终用法的原因,但没有给出用法的必要性。

From javadoc for Thread : 从javadoc中获取Thread

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. 根据系统计时器和调度程序的精度和准确性,使当前正在执行的线程进入休眠状态(暂时停止执行)达指定的毫秒数。 The thread does not lose ownership of any monitors. 该线程不会失去任何监视器的所有权。

Don't mind that the sleep() method is static, it is executed for the thread in which you call it. 不要介意sleep()方法是静态的,它在您调用它的线程中执行。 So if you call it from GUI, it puts your main UI thread into sleep, hence freezing UI completely. 因此,如果从GUI调用它,它将使主UI线程进入睡眠状态,从而完全冻结UI。

by default the program does not have more than one thread. 默认情况下,该程序没有多个线程。 there should be only one class which has a main method. 应该只有一个具有main方法的类。 it does not matter it is a ui application or any other type of application. 不管是ui应用程序还是任何其他类型的应用程序都没有关系。

please post the details in what scenario you want to use the sleep function. 请在您要使用睡眠功能的情况下发布详细信息。

When a java class is created automatically 3 thread are created by-default with it. 自动创建Java类时,默认情况下会使用它创建3个线程。 1.Main Thread 2.Thread Scheduler 3.Garbage Collector thread 1.主线程2.线程调度器3.垃圾收集器线程

Please watch this explanatory video to understand what a Thread actually is. 请观看此说明性视频,以了解线程实际上是什么。

In this example the Thread is the music box, your code is the sheet of paper, and the processing time granted to a thread is the rotation of the handle. 在此示例中,“线程”是音乐盒,您的代码是“纸”,授予线程的处理时间是手柄的旋转。

So your main-class code (sheet-of-paper) is inserted into a thread (music-box) generated by the JVM and execution is started (handle rotates). 因此,将您的主类代码(纸页)插入到JVM生成的线程(音乐盒)中,并开始执行(句柄旋转)。 Once you call sleep(1000) "rotations of the handle" is paused for 1 second, and so is the execution of your code. 一旦调用sleep(1000) “句柄旋转”将暂停1秒钟,代码执行也将暂停1秒钟。 Thread.sleep() is just a short-cut to Thread.getCurrentThread().sleep() . Thread.sleep()只是Thread.getCurrentThread().sleep()

If you now have several threads (music-boxes), you can of course pause one while another still runs. 如果现在有多个线程(音乐盒),则可以暂停一个线程,而另一个线程仍在运行。 So if you have a main thread and a GUI thread, you can pause the main thread and your GUI will work fine (unless it actually tries to access the main thread). 因此,如果您有一个主线程和一个GUI线程,则可以暂停该主线程,并且GUI可以正常工作(除非它实际上试图访问该主线程)。 If you however pause the GUI thread, it will appear to be frozen. 但是,如果您暂停GUI线程,它将似乎被冻结。

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

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