Java
(1)通过扩展Thread类来创建多线程
class MutliThread extends Thread{
private int ticket=100;//每个线程都拥有100张票
MutliThread(Stringname){
super(name);//调用父类带参数的构造方法
}
public void run(){
while(ticket>0){
System.out.println(ticket--+"is saled by "+Thread.currentThread().getName());
}
}
}
(2)通过实现Runnable接口来创建多线程
class MutliThread implements Runnable{
private int ticket=100;//每个线程都拥有100张票
private String name;
MutliThread(Stringname){
this.name=name;
}
public void run(){
while(ticket>0){
System.out.println(ticket--+"is saled by "+name);
}
}
}