⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synthreadexam1.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 JAVA
字号:

class TicketThread1
    extends Thread {
  private static int tickets = 10;
  private String str;
  public TicketThread1(String str) {
    this.str = str;
  }

  public void run() {
    while (true) {
      synchronized (str) {
        if (tickets > 0) {
          try {
            sleep(100);
          }
          catch (Exception e) {
            System.err.println(e.getMessage());
          }
          System.out.println(this.getName() + " is saling " + tickets--);
        }
        else
          return;
      }
    }
  }
}

public class SynThreadExam1 {
  public static void main(String[] args) {
    TicketThread1[] t = new TicketThread1[4];
    String str = new String("test");
    for (int i = 0; i < 4; i++) {
      t[i] = new TicketThread1(str);
      t[i].start();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -