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

📄 multithreaddemo2.java

📁 Java程序设计大学教程程序源代码
💻 JAVA
字号:
/* * MultiThreadDemo2.java * 参见教程284页 示例程序10-2 * Created on 2005年8月14日, 下午5:18 */package jbookch10;public class MultiThreadDemo2 {    public static void main(String args[]){        //运行子线程        Thread t1,t2,t3,t4;        t1=new Thread(new NewSleepThread("1"));        t1.start();//启动线程1        t2=new Thread(new NewSleepThread("2"));        t2.start();//启动线程2        t3=new Thread(new NewSleepThread("3"));        t3.start();//启动线程3        t4=new Thread(new NewSleepThread("4"));        t4.start();//启动线程4        //运行主线程        System.out.print(" 主线程启动! ");        try {            for (int i=0;i<100; i++) {                System.out.print("  -->  ");                Thread.sleep(20);            }        } catch (InterruptedException e) {            System.out.print(" 主线程中断! ");        }        System.out.print(" 主线程结束! ");    }}class NewSleepThread implements Runnable {    NewSleepThread(String threadName) {        name=threadName;        System.out.print(" 新线程"+name+"启动! ");    }        //线程入口    public void run() {        try {            for (int i=0;i<100; i++) {                System.out.print(name);                Thread.sleep(20);            }        } catch (InterruptedException e) {            System.out.print(" 线程"+name+"中断! ");        }        System.out.print(" 线程"+name+"结束! ");    }        private String name;    }

⌨️ 快捷键说明

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