class numThread extends Thread{
public numThread(){
// t.start();
}
Thread t=new Thread();
public void run(){
try{
for(int i=300;i>0;i--){
Thread.sleep(1000);
System.out.println(i);
while(i==0){
i=300;
}
}
}catch(Exception e){
System.out.println("Exception");
}
}
public static void main(String args[]){
new numThread().start;
}
}
public numThread(){
// t.start();
}
Thread t=new Thread();
public void run(){
try{
for(int i=300;i>0;i--){
Thread.sleep(1000);
System.out.println(i);
while(i==0){
i=300;
}
}
}catch(Exception e){
System.out.println("Exception");
}
}
public static void main(String args[]){
new numThread().start;
}
}
while(i==0){
i=300;
}
这个怎么这样写呢?if( i == 0 )
i = 300;
不就行了吗?Java可不是英语啊
i = 300;
==================
多此一举吧...class numThread extends Thread{
Thread t=new Thread();
public void run(){
try{
for(int i=300;i>0;i--){
Thread.sleep(1000);
System.out.println(i);
}
}catch(Exception e){
System.out.println("Exception");
}
}
public static void main(String args[]){
numThread t=new numThread();
t.start();
}
}
所以
==================
if( i == 0 )
i = 300;
public numThread(){
t.start();
}
Thread t=new numThread ();//构造成你自己的类才能让它跑起来
public void run(){
try{
for(int i=300;i>0;i--){
Thread.sleep(1000);
System.out.println(i);
//while(i==0){
// i=300;//你不是要循环,何必这样写呢
//}
if(i==0)
i=300;
}
}catch(Exception e){
System.out.println("Exception");
}
}
public static void main(String args[]){
new numThread();
}
}
extends Thread {
private static int n = 300; private synchronized static int getAndSub() {
if (n <= 0)
return 0;
return n--;
} public synchronized static int get() {
return n;
} private Latch l;
private CountDown c;
public NumThread(Latch l, CountDown c) {
if (l == null || c == null) {
throw new NullPointerException();
} this.l = l;
this.c = c;
} public void run() {
try {
try {
l.acquire();
}
catch (InterruptedException ie) {
ie.printStackTrace();
return;
} while (true) {
int n = getAndSub();
if (n <= 0)
break; System.out.println(n);
try {
Thread.sleep( (int) (1000 * 0.1 * Math.random()));
}
catch (InterruptedException ie) {
}
}
}
finally {
this.c.release();
}
} public static void main(String[] args) throws InterruptedException {
int threadCount = 10; Latch l = new Latch();
CountDown c = new CountDown(threadCount);
for (int i = 0; i < threadCount; i++) {
new NumThread(l, c).start();
} System.out.println("starting...");
l.release(); c.acquire();
System.out.println("done.");
}
}
class numThread extends Thread{
Thread t=new Thread();
public void run(){
try{
for(int i=10;i>=0;i--){
Thread.sleep(1000);
System.out.println(i);
if(i==0){//这里可以用while;
i=11;//这里改成11,因为有i--,否则就是9了;
}
}
}catch(Exception e){
System.out.println("Exception");
}
}
public static void main(String args[]){
numThread t=new numThread();
t.start();
}
}
<<
while(true) {
for(int i=300;i>=0;i--) {
// ....
}
}
>>
//多线程输出300到0public class NumThread
{
int i=300;
NumThread2 nt2;
NumThread()
{
nt2= new NumThread2(NumThread.this);
while(true)
{
try
{Thread.sleep(500);
System.out.println("Thread1 output : i="+i);
if(i>0) i--;
else System.exit(0);
}
catch (InterruptedException e){}
}
}
public static void main(String[] args)
{
NumThread nt=new NumThread();
}
}class NumThread2 extends Thread {
NumThread nt1;
public NumThread2(NumThread nt1) {
this.nt1 = nt1;
start();
}
public void run() {
while(true)
{
try
{Thread.sleep(500);
System.out.println("Thread2 output : i="+nt1.i);
if(nt1.i>0) nt1.i--;
else System.exit(0);
}
catch (InterruptedException e){}
}
}
}