• Tags
  •         
  • www.breakyizhan.com
  •    

    多线程是一种Java功能,允许并发执行程序的两个或多个部分,以最大限度地利用CPU。这种程序的每个部分都称为线程。因此,线程是一个过程中的轻量级进程。

    可以使用两种机制创建线程:
    1. 扩展Thread类
    2. 实现Runnable接口

    通过扩展Thread类创建线程

    创建线程我们创建一个扩展java.lang.Thread类的类。此类重写Thread类中可用的run()方法。线程在run()方法中开始生命。我们创建了一个新类的对象,并调用start()方法来开始执行一个线程。Start()调用Thread对象上的run()方法。

    // Java code for thread creation by extending
    // the Thread class
    class MultithreadingDemo extends Thread
    {
        public void run()
        {
            try
            {
                // Displaying the thread that is running
                System.out.println ("Thread " +
                      Thread.currentThread().getId() +
                      " is running");
    
            }
            catch (Exception e)
            {
                // Throwing an exception
                System.out.println ("Exception is caught");
            }
        }
    }
    
    // Main Class
    public class Multithread
    {
        public static void main(String[] args)
        {
            int n = 8; // Number of threads
            for (int i=0; i<8; i++)
            {
                MultithreadingDemo object = new MultithreadingDemo();
                object.start();
            }
        }
    }
    

    输出:

    Thread 8 is running
    Thread 9 is running
    Thread 10 is running
    Thread 11 is running
    Thread 12 is running
    Thread 13 is running
    Thread 14 is running
    Thread 15 is running

    通过实现Runnable接口创建线程

    创建线程我们创建了一个新类,它实现了java.lang.Runnable接口并覆盖了run()方法。然后我们实例化一个Thread对象并在该对象上调用start()方法。

    // Java code for thread creation by implementing
    // the Runnable Interface
    class MultithreadingDemo implements Runnable
    {
        public void run()
        {
            try
            {
                // Displaying the thread that is running
                System.out.println ("Thread " +
                                    Thread.currentThread().getId() +
                                    " is running");
    
            }
            catch (Exception e)
            {
                // Throwing an exception
                System.out.println ("Exception is caught");
            }
        }
    }
    
    // Main Class
    class Multithread
    {
        public static void main(String[] args)
        {
            int n = 8; // Number of threads
            for (int i=0; i<8; i++)
            {
                Thread object = new Thread(new MultithreadingDemo());
                object.start();
            }
        }
    }
    

    输出:

    Thread 8 is running
    Thread 9 is running
    Thread 10 is running
    Thread 11 is running
    Thread 12 is running
    Thread 13 is running
    Thread 14 is running
    Thread 15 is running

     

    线程类与Runnable接口

    1.如果我们扩展Thread类,我们的类不能扩展任何其他类,因为Java不支持多重继承。但是,如果我们实现Runnable接口,我们的类仍然可以扩展其他基类。

    2.我们可以通过扩展Thread类来实现线程的基本功能,因为它提供了一些内置的方法,如yield(),interrupt()等在Runnable接口中不可用。

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