package xc;
import java.util.*;
public class TextInterrupt {
class MyThread implements Runnable {
public void run(){
while(true){
System.out.println("------"+new Date()+"------");
}
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
public static void main(String[] args) {
MyThread thread=new MyThread();
Thread t=new Thread(thread);
t.start();
try{
sleep(t.sleep(10000);)
}catch(InterruptedException e){
t.interrupt();
}
}
}
import java.util.*;
public class TextInterrupt {
class MyThread implements Runnable {
public void run(){
while(true){
System.out.println("------"+new Date()+"------");
}
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
public static void main(String[] args) {
MyThread thread=new MyThread();
Thread t=new Thread(thread);
t.start();
try{
sleep(t.sleep(10000);)
}catch(InterruptedException e){
t.interrupt();
}
}
}
解决方案 »
- java 泛刑编程 作两个数比较
- 如何实现一个JTabbedPane的子面板都为分割面板?
- 帮忙分析一下哪些是正确的" is a"关系
- 用java语言怎么实现文件夹打开(用java代码)
- Image inImage = new ImageIcon("/img/comm/bg1.jpg").getImage();出错,急。。。。
- 请教:如何用java语言实现把一段字符串中的字符从little-endian转换成big-endian顺序?
- 高分求救啊!!!!高手们快来救救我啊!!!关于applet显示图片更新的问题
- 新手的问题,请教各位
- Exception in thread "main" java.lang.NoClassDefFoundError:Ellsworth(这是我的文件)。上面是我编译通过之后,执行是出现的信息?哪里
- 5分钟,得100分,来者有分
- java类实例化遇到的问题。求大神讲解下原理
- java swing 中给table添加右键菜单
MyThread实现Runnable接口.这个接口对应的只有run()方法.没有sleep方法.所以你在run中调用sleep会报错.
另外你意思在while(true)中输出.然后把线程sleep()方法放到while循环外,这样也过不了编译.还有就是sleep()作为Thread的一个静态方法,没必要用实例对象来调用.
所以我实在不明白你在main中的 sleep(t.sleep(10000);) 这句代码想要实现的目的是什么.上面的错误,先把编译这关过了,如果编译都过不了就谈不上执行了
sleep方法是Thread类独有的,这里继承Runnable接口类本身就没有sleep方法了。
sleep是Thread类的静态方法。sleep的作用是让线程休眠制定的时间,在时间到达时恢复,也就是说sleep将在接到时间到达事件事恢复线程执行。
你的这个代码把两个sleep(1000)、sleep(t.sleep(10000);)可以改写为Thread.sleep(1000)和Thread.sleep(10000)可能就行。
Thread.currentThread().sleep(1000);这样调用的