⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examplethreads.java

📁 基于网络编程的例子
💻 JAVA
字号:
// File: ExampleThreads.java
//
// Example of using Java threads
//

import java.io.*;
import java.util.zip.*;

class MyThread extends Thread {
    public MyThread(String name) {
	super(name);
	this.name = name;
    }
    public MyThread() {
	this("no_name_thread");
    }
    private String name;
	
    public void run() {
	for (int i=0; i<5; i++) {
	    System.out.println(name + " " + i);
	    try {
		Thread.sleep(1000); // sleep 1 second
	    } catch (Exception e) {
		System.out.println("Thread sleep exception: " + e);
	    }
	}
    }
}

public class ExampleThreads {

    static public void main(String [] args) {

	// make an array of MyThread objects:
	MyThread threads[] = new MyThread[10];
	for (int i=0; i<10; i++) {
	    threads[i] = new MyThread("my_thread_" + i);
	}
	// now start all the threads:
	for (int i=0; i<10; i++) {
	    threads[i].start(); // calls the 'run' method
	}
    }
}

⌨️ 快捷键说明

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