运行多线程的 Java 程序

原文:https://www . geesforgeks . org/Java-程序转运行-多线程/

螺纹是轻量化工艺。执行中的进程称为程序。程序的子部分称为线程。线程允许一个程序通过同时做多件事,在后台执行复杂的任务,而不中断主程序的执行,从而更有效地运行。

所有线程都可以相互通信。Java 提供了一个 Thread 类来实现线程编程。Thread 类提供构造函数和方法来创建和执行线程上的操作。

各种穿线方法:

  • start():方法用于启动线程的执行。
  • run():方法用于执行操作。
  • sleep():这个方法让一个线程休眠指定的时间。
  • resume():此方法用于恢复挂起的线程。
  • stop():此方法用于停止线程。
  • destroy():此方法用于销毁线程组及其所有子组。

语法:

公共类线程 扩展对象 实现 Runnable

Java 语言(一种计算机语言,尤用于创建网站)

// Java Program to Run Multiple Threads

// class extends thread class
class Main extends Thread {

    // run method implementation
    public void run()
    {
        System.out.println("Geeks for Geeks");
    }

    // in the main method
    public static void main(String args[])
    {
        // object creation
        Main t1 = new Main();

        // object creation
        Main t2 = new Main();

        // object creation
        Main t3 = new Main();

        // start the thread
        t1.start();

        // start the thread
        t2.start();

        // start the thread
        t3.start();
    }
}

Output

Geeks for Geeks
Geeks for Geeks
Geeks for Geeks