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

📄 morethreads.java

📁 压缩包内是近180多个针对Java初学者编写的简单实例
💻 JAVA
字号:
//创造多线程
class MyThread implements Runnable{
	int count;
	Thread thrd;
	
	//创造一个新的线程/
	MyThread(String name){
		thrd = new Thread(this, name);
		count = 0;
		thrd.start();//start the thread.
	}
	
	//beginning execution of new thread.
	public void run(){
		System.out.println(thrd.getName()+" 线程开始.");
		try{
			do{
				Thread.sleep(500);
				System.out.println("在 "+ thrd.getName()+
						", 计数到 "+count);
				count++;
			}while(count<5);
		}catch(InterruptedException e){
			System.out.println(thrd.getName()+" 被打断.");
		}
		System.out.println(thrd.getName()+ " 结束.");
	}
}
public class MoreThreads {
	public static void main(String args[]){
		System.out.println("主线程开始:");
		MyThread mt1= new MyThread("线程 1");
		MyThread mt2 = new MyThread("线程 2");
		MyThread mt3 = new MyThread("线程 3");
		
		do{
			System.out.print(".");
			try{
				Thread.sleep(100);
			}catch(InterruptedException e){
				System.out.println("主线程被打断.");
			}
		}while((mt1.count<5)&&(mt2.count<5)&&(mt3.count<5));
		System.out.println("主线程结束.");
	}
}

⌨️ 快捷键说明

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