java_thread

多线程代码

package _01thread._01MovieTick;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Station extends Thread {
    public Station(String name) {
        super(name);
    }
    static int tick =1;
    //todo  this step is very importent
    Lock lock=new ReentrantLock();
    @Override
    public void run() {

        lock.lock();
            try {
                while(tick<=100){
                    System.out.println(Thread.currentThread().getName()+"  "+tick+" 张票");
                    tick++; }
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
            lock.unlock();
        }

    }
}

代码实现

package _01thread._01MovieTick;

public class MainClass {
    public static void main(String[] args) {
        Station station1 = new Station("窗口A ");
        Station station2 = new Station("窗口B ");
        Station station3 = new Station("窗口C ");
        station1.start();
        station2.start();
        station3.start();
    }
}

你可能感兴趣的:(JAVA)