为什么这段程序执行后CPU的占用率就特别高呢 到90%多- package com.heima.thread;
- /**
- * 需求:简单的卖票程序,模拟4个线程同时卖100张票。多窗口同时卖票
- * @author Administrator
- *
- */
- class Ticket extends Thread{
- private int ticket=100;
- public void run()
- {
- while(true)
- {
- if(ticket>0)
- {
- System.out.println(this.getName()+"…………"+ticket--);
- }
- }
- }
- }
- public class ThreadTicket {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Ticket t1=new Ticket();
- Ticket t2=new Ticket();
- Ticket t3=new Ticket();
- Ticket t4=new Ticket();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码
|
|