- public class Test {
- public static void main(String[] args) {
- Tick tick = new Tick();
- new Thread(tick).start();
- new Thread(tick).start();
- new Thread(tick).start();
- }
- }
- class Tick implements Runnable{
- private int tick=300;
- public void run(){
- while(true){
- if(tick>0){
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName()+" "+tick--);
- }
- }
- }
- }
复制代码 我复习了下多线程的那块,但是卖票的那个程序,不写同步的话结尾出现0或者负数,这个我知道怎么解释,但是出现多个线程卖同一张票,应该怎么解释原因呢?
|
|