本帖最后由 韩天雷 于 2014-7-13 01:48 编辑
代码执行完后 javaw.exe未退出并大量占用CPU
不知道原因~
代码如下,求解答~- class Ticket implements Runnabl{
- private int tick = 100;
- Object p =new Object();
- public void run() {
- while (true) {
- synchronized (p) {
- if (tick > 0) {
- try {
- Thread.sleep(10);
- } catch (Exception e) {
- }
- System.out.println(Thread.currentThread().getName() + "卖出"
- + tick-- + "号票!");
- }
- }
- }
- }
- }
- public class TicketDemo {
- public static void main(String[] args) {
- Ticket t = new Ticket();
- Thread t1 = new Thread(t, "1号窗口");
- Thread t2 = new Thread(t, "2号窗口");
- Thread t3 = new Thread(t, "3号窗口");
- Thread t4 = new Thread(t, "4号窗口");
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码
|
|