package com.thread.runnable;
/**
* 实现多线程的方式有继承Thread类和实现Runnable接口两种方式
* 哪种方式更好呢?实现的方式由于继承的方式。
* 原因:1.避免了Java但继承的局限性
* 2.如果多个线程要操作同一份资源,实现接口的方式更适合
* @author Administrator
*
*/
public class TestRunnable {
public static void main(String[] args) {
// OddTest odd = new OddTest();
// EventTest event = new EventTest();
// new Thread(odd,"奇数").start();
// new Thread(event,"偶数").start();
TestTicket ticket = new TestTicket();
new Thread(ticket,"窗口一").start();;
new Thread(ticket,"窗口二").start();;
new Thread(ticket,"窗口三").start();;
}
}
class TestTicket implements Runnable{
int ticket = 100;
@Override
public void run() {
while(true){
if( ticket > 0){
System.out.println(Thread.currentThread().getName()+"正在售票:"+ticket --);
}else{
break;
}
}
}
}
class OddTest implements Runnable{
@Override
public void run() {
for(int i = 1; i <= 100; i++){
if( i % 2 == 0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
class EventTest implements Runnable{
@Override
public void run() {
for(int i = 1; i <= 100; i++){
if( i % 2 != 0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}