继承 package thread;
class Ticket extends Thread
{
private static int tike=100;
public void run()
{
while(tike>0)
{
try {
sleep(20); // 不用Thread.就能调用
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" run "+tike--);
}
}
}
public class TicketDemo {
/**
* @param args
*/
public static void main(String[] args) {
Ticket ti=new Ticket();
Ticket t1=new Ticket();
Ticket t2=new Ticket();
Ticket t3=new Ticket();
ti.start();
t1.start();
t2.start();
t3.start();
}
}
实现package thread;
class Ticke implements Runnable
{
private int tik=100;
public void run() {
while(tik>0)
{
try {
Thread.sleep(20); // 必须Thread. 来调用
. } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" run="+tik--);
}
}
}
public class Runnable_1 {
/**
* @param args
*/
public static void main(String[] args) {
Ticke t=new Ticke();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
问什么在实现Runnable中,sleep()必须由Thread. 来调用 |