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

📄 threaddemo.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// ThreadDemo.java - Demonstrates threaded programming
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import java.io.IOException;

// Extend Thread class and provide a run() method
class Background extends Thread {

 boolean finished;  // True when thread should die

 // Constructor
 Background(String name) {
  super(name);
  finished = false;  // Initialize run-flag
 }

 // Called by start() to run the thread
 public void run() {
  try {
   while (!finished) {   // Loop "forever"
    sleep(2000);
    System.out.println("\nHurry up!");
    sleep(1000);
    System.out.println("\nWhat's taking you so long?");
    sleep(1500);
    System.out.println("\nC'mon, press that Enter key!");
   }
  } catch (InterruptedException e) {
   return;  // End the thread
  }
 }

 // Halt the thread
 public void halt() {
  finished = true;  // Cause while loop in run() to end
  System.out.println("Stopping thread " + getName() + "\n");
 }
}

// Main program class demonstrates background processing
class ThreadDemo {
 public static void main(String args[]) {

// Create the background thread
  Background background = 
   new Background("Background process");
  System.out.println(
    "Starting thread. Press Enter to stop.");

// Start running the background thread
  background.start();

// Simulate foreground process: wait for Enter key
  try {
   while ((char)System.in.read() != '\n') ;
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }

// Stop the background thread
  background.halt();
 }
}

⌨️ 快捷键说明

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