• Tags
  •         
  • www.breakyizhan.com
  •    

    Java为多线程编程提供内置支持。多线程程序包含两个或多个可以并发运行的部分。这样的程序的每个部分称为线程,每个线程定义一个单独的执行路径。

    Java中的主线程

    当Java程序启动时,一个线程立即开始运行。这通常被称为我们程序的线程,因为它是我们程序开始时执行的线程。

    属性:

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

    流程图 :


    Java中的主线程

    如何控制Java中的主线程

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

    // Java program to control the Main Thread
    public class Test extends Thread
    {
        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);
            
            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
            ChildThread ct = new ChildThread();
            
            // 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();
        }
    }
    
    // Child Thread class
    class ChildThread extends Thread
    {
        @Override
        public void run()
        {
            for (int i = 0; i < 5; i++)
            {
                System.out.println("Child thread");
            }
        }
    }
    

    输出:

    Current thread: main
    After name change: Geeks
    Main thread priority: 5
    Main thread new priority: 10
    Main thread
    Main thread
    Main thread
    Main thread
    Main thread
    Child thread priority: 10
    Child thread new priority: 1
    Child thread
    Child thread
    Child thread
    Child thread
    Child thread

    Java中main()方法和主线程之间的关系

    对于每个程序,主线程由JVM(Java虚拟机)创建。“Main”线程首先验证main()方法的存在,然后初始化类。请注意,在JDK 6中,main()方法在独立的Java应用程序中是必需的。

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

    我们可以通过使用Main线程来创建死锁,即只使用一个线程。以下java程序演示了这一点。

    // Java program to demonstrate deadlock
    // using Main thread
    public class Test
    {
        public static void main(String[] args)
        {
            try
            {
                
                System.out.println("Entering into Deadlock");
                
                Thread.currentThread().join();
                
                // the following statement will never execute
                System.out.println("This statement will never execute");
                
            }
            
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    输出:

    Entering into Deadlock
    

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

     
    转载请保留页面地址:https://www.breakyizhan.com/java/4935.html