class text extends Thread
{
static int s=100;
public synchronized void run()
{
while (true) {
if(s>0) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s--;
System.out.println("还有"+s+"张票");
}
}
}
}
public class Dss {
public static void main(String[] args) {
text a=new text();
text b=new text();
a.start();
b.start();
}
}
这两个的区别Thread是我new了好几个对象,所以要不static 让他们使用一个对象,Runnable是new了一个对象给多个thread所以不用static
package gay;
class text implements Runnable
{
int s=100;
public synchronized void run()
{
while (true) {
if(s>0) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s--;
System.out.println("还有"+s+"张票");
}
}
}
}
public class Dss {
public static void main(String[] args) {
text a=new text();
Thread aa=new Thread(a);
Thread bb=new Thread(a);
aa.start();
bb.start();
}
}