一个线程的题目,基础最重要 编写一个程序,能够生成n个线程,每个线程每隔1秒打印自己的序号,当用户输入m后第m个线程结束. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.Scanner;public class ThreadTest extends Thread { private int index; private boolean go; public ThreadTest(int index) { super(); this.index=index; go=true; } public int getIndex() { return index; } public void stopRun() { go=false; } public void run() { while(go) { System.out.println(index); try { sleep(1000); }catch(InterruptedException e){e.printStackTrace();}; } } public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.println("input N:"); int n = scanner.nextInt(); if(n<=0)return; int m; ThreadTest[] threads=new ThreadTest[n]; for(int i=0 ; i<n ; i++) { threads[i]=new ThreadTest(i); threads[i].start(); } while(true) { m = scanner.nextInt(); if(m<0) { for(int i=0 ; i<n ; i++) { threads[i].stopRun(); break; } } else if(m>=n)continue; threads[m].stopRun(); } }} 两道外企面试题~求解 急!!!急!!!Spring+Hibernate的配置文件 问题????? web-inf的class下怎么没有文件呀 unreachable statement ?? try/catch的问题 在用到JFrame时遇到的一个问题 数据库连接问题(困扰我好久了) 曾经有一个socket例子,但我没有珍惜...... 百分比问题 我jbuilder6里的tools->create client jar菜单不可用,这是什么原因 java系统中会出现内存泄漏的危险么 help me,寻找新版的jxl.jar
{
private int index;
private boolean go;
public ThreadTest(int index)
{
super();
this.index=index;
go=true;
} public int getIndex()
{
return index;
} public void stopRun()
{
go=false;
} public void run()
{
while(go)
{
System.out.println(index);
try
{
sleep(1000);
}catch(InterruptedException e){e.printStackTrace();};
}
} public static void main(String[] args)
{
Scanner scanner= new Scanner(System.in);
System.out.println("input N:");
int n = scanner.nextInt(); if(n<=0)return; int m;
ThreadTest[] threads=new ThreadTest[n]; for(int i=0 ; i<n ; i++)
{
threads[i]=new ThreadTest(i);
threads[i].start();
} while(true)
{
m = scanner.nextInt();
if(m<0)
{
for(int i=0 ; i<n ; i++)
{
threads[i].stopRun();
break;
}
}
else if(m>=n)continue;
threads[m].stopRun();
}
}
}