Java中的主线程

Java为多线程编程提供了内置支持。多线程程序包含两个或多个可以同时运行的部分。这样一个程序的每一部分都被称为线程,每个线程定义一个单独的执行路径。 当Java程序启动时,一个线程立即开始运行。这通常被称为 主要的 线程,因为它是程序开始时执行的线程。

null

与主线程相关的某些属性如下:

  • 它是其他“子”线程将从中派生的线程。
  • 通常,它必须是最后一个完成执行的线程,因为它执行各种关闭操作

流程图如下:

main thread in java

如何控制主线程

主线程是在程序启动时自动创建的。为了控制它,我们必须获得对它的引用。这可以通过调用方法来实现 当前线程() 这在线程类中存在。此方法返回对调用它的线程的引用。主线程的默认优先级为5,对于所有剩余的用户线程,优先级将从父线程继承到子线程。

实例

JAVA

// Java program to control the Main Thread
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Main class extending thread class
public class Test extends Thread {
// Main driver method
public static void main(String[] args)
{
// Getting reference to Main thread
Thread t = Thread.currentThread();
// Getting name of Main thread
System.out.println( "Current thread: "
+ t.getName());
// Changing the name of Main thread
t.setName( "Geeks" );
System.out.println( "After name change: "
+ t.getName());
// Getting priority of Main thread
System.out.println( "Main thread priority: "
+ t.getPriority());
// Setting priority of Main thread to MAX(10)
t.setPriority(MAX_PRIORITY);
// Print and display the main thread priority
System.out.println( "Main thread new priority: "
+ t.getPriority());
for ( int i = 0 ; i < 5 ; i++) {
System.out.println( "Main thread" );
}
// Main thread creating a child thread
Thread ct = new Thread() {
// run() method of a thread
public void run()
{
for ( int i = 0 ; i < 5 ; i++) {
System.out.println( "Child thread" );
}
}
};
// Getting priority of child thread
// which will be inherited from Main thread
// as it is created by Main thread
System.out.println( "Child thread priority: "
+ ct.getPriority());
// Setting priority of Main thread to MIN(1)
ct.setPriority(MIN_PRIORITY);
System.out.println( "Child thread new priority: "
+ ct.getPriority());
// Starting child thread
ct.start();
}
}
// Class 2
// Helper class extending Thread class
// Child Thread class
class ChildThread extends Thread {
@Override public void run()
{
for ( int i = 0 ; i < 5 ; i++) {
// Print statement whenever child thread is
// called
System.out.println( "Child thread" );
}
}
}


输出

Current thread: mainAfter name change: GeeksMain thread priority: 5Main thread new priority: 10Main threadMain threadMain threadMain threadMain threadChild thread priority: 10Child thread new priority: 1Child threadChild threadChild threadChild threadChild thread

现在,让我们讨论一下Java中main()方法和主线程之间的关系。 对于每个程序,一个主线程由 JVM (Java虚拟机)。“Main”线程首先验证Main()方法的存在,然后初始化该类。注意,在JDK 6中,main()方法在独立java应用程序中是必需的。

使用主线程的死锁(仅单线程)

我们可以只使用主线程来创建死锁,也就是说,只使用一个线程。

实例

JAVA

// Java program to demonstrate deadlock
// using Main thread
// Main class
public class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check for exceptions
try {
// Print statement
System.out.println( "Entering into Deadlock" );
// Joining the current thread
Thread.currentThread().join();
// This statement will never execute
System.out.println( "This statement will never execute" );
}
// Catch block to handle the exceptions
catch (InterruptedException e) {
// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
}


输出:

图片[2]-Java中的主线程-yiteyi-C++库

输出说明: 语句“Thread.currentThread().join()”将告诉主线程等待该线程(即等待自身)死亡。因此,主线程等待自己消亡,这只不过是一个死锁。

相关文章: Java中的守护进程线程 . 本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享