multithread.java

来自「由于想一次上传成功 所以将二个教程打包成了一个 这些例子几乎包含了所有java的」· Java 代码 · 共 43 行

JAVA
43
字号
class CustomThread extends Thread 
{
    CustomThread(String name) 
    {
        super(name);
        start(); 
    }

    public void run() 
    {
        try {
            for(int loop_index = 0; loop_index < 4; loop_index++) {
                System.out.println((Thread.currentThread()).getName() 
                    + " thread here...");
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {}

        System.out.println((Thread.currentThread()).getName() + " ending.");
    }
}

class multithread
{
    public static void main(String args[]) 
    {
        CustomThread thread1 = new CustomThread("first"); 
        CustomThread thread2 = new CustomThread("second"); 
        CustomThread thread3 = new CustomThread("third"); 
        CustomThread thread4 = new CustomThread("fourth"); 

        try {
            for(int loop_index = 0; loop_index < 10; loop_index++) {
                System.out.println((Thread.currentThread()).getName() 
                    + " thread here...");
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {}
    }
}
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?