java 關閉一個正在執行的執行緒
- publicstaticvoid main(String[] args) throws Exception {
- MyThread mt =
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- //執行緒沒有處於阻塞狀態,呼叫執行緒對應的interrupt()不能讓執行的執行緒停止下來
- t.interrupt();
- }
- staticclass MyThread implements Runnable {
- publicvolatileboolean stop = false;
- privatevoid dosomethig() throws InterruptedException {
- long time = System.currentTimeMillis();
- while(System.currentTimeMillis() - time < 1000) {
- }
- System.out.println("all things had been done!!"
- }
- @Override
- publicvoid run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 執行結果:
- System is ready to start thread
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- System is ready to stop thread
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- publicstaticvoid main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- // t.interrupt();
- //當執行緒沒有處於阻塞狀態,通過改變標誌量,可以讓執行緒停止執行
- mt.stop = true;
- }
- 執行結果:
- System is ready to start thread
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- all things had been done!!
- Thread-0 is running..
- System is ready to stop thread
- all things had been done!!
- Thread-0 is exiting under request.
- publicstaticvoid main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- // t.interrupt();
- //此時執行緒一直處於阻塞狀態,無法檢查標誌量,所以僅通過改變標誌量無法停止執行緒
- mt.stop = true;
- }
- staticclass MyThread implements Runnable {
- publicvolatileboolean stop = false;
- privatevoid dosomethig() throws InterruptedException {
- // long time = System.currentTimeMillis();
- // while(System.currentTimeMillis() - time < 1000) {
- //
- // }
- Thread.currentThread().join();
- System.out.println("all things had been done!!");
- }
- @Override
- publicvoid run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 執行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
- publicstaticvoid main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- //通過呼叫執行緒物件上的interrupt() 正在執行的執行緒物件會接收到一個InterruptedException異常,從而停止執行
- t.interrupt();
- // mt.stop = true;
- }
- 執行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
- java.lang.InterruptedException
- Thread-0 is exiting under request.
- at java.lang.Object.wait(Native Method)
- at java.lang.Thread.join(Thread.java:1143)
- at java.lang.Thread.join(Thread.java:1196)
- at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:29)
- at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:38)
- at java.lang.Thread.run(Thread.java:619)
- publicstaticvoid main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- Thread t = new Thread(mt);
- System.out.println("System is ready to start thread");
- t.start();
- Thread.sleep(3000);
- System.out.println("System is ready to stop thread");
- t.interrupt();
- mt.stop = true;
- mt.socket.close();
- }
- staticclass MyThread implements Runnable {
- publicvolatileboolean stop = false;
- ServerSocket socket = null;
- privatevoid dosomethig() throws InterruptedException, IOException {
- // long time = System.currentTimeMillis();
- // while(System.currentTimeMillis() - time < 1000) {
- //
- // }
- // Thread.currentThread().join();
- socket = new ServerSocket(9999);
- //這裡需要呼叫Socket對應的close方法 這樣子 被阻塞的執行緒會接收到一個SocketException 從而停止執行
- socket.accept();
- System.out.println("all things had been done!!");
- }
- @Override
- publicvoid run() {
- try {
- while(!stop) {
- System.out.println(Thread.currentThread().getName() + " is running..");
- dosomethig();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- System.out.println(Thread.currentThread().getName() + " is exiting under request.");
- }
- }
- }
- 執行結果:
- System is ready to start thread
- Thread-0 is running..
- System is ready to stop thread
- java.net.SocketException: socket closed
- Thread-0 is exiting under request.
- at java.net.PlainSocketImpl.socketAccept(Native Method)
- at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
- at java.net.ServerSocket.implAccept(ServerSocket.java:453)
- at java.net.ServerSocket.accept(ServerSocket.java:421)
- at com.thread.DeadLockTest$MyThread.dosomethig(DeadLockTest.java:33)
- at com.thread.DeadLockTest$MyThread.run(DeadLockTest.java:42)
- at java.lang.Thread.run(Thread.java:619
相關推薦
java 關閉一個正在執行的執行緒
中斷(Interrupt)一個執行緒意味著在該執行緒完成任務之前停止其正在進行的一切,有效地中止其當前的操作。執行緒是死亡、還是等待新的任務或是繼續執行至下一步,就取決於這個程式。雖然初次看來它可能顯得簡單,但是,你必須進行一些預警以實現期望的結果。你最好還是牢記以下的幾
Java 輸入一個正整數,將其用中文打印出來
輸入一個正整數,將其用中文打印出來。使用java 實現 import java.util.Scanner; public static void main(String[] arge){
今天開始學Java 給定一個正整數,編寫程式計算有多少對質數的和等於輸入的這個正整數,並輸出結果。
給定一個正整數,編寫程式計算有多少對質數的和等於輸入的這個正整數,並輸出結果。輸入值小於1000。如,輸入為10, 程式應該輸出結果為2。(共有兩對質數的和為10,分別為(5,5),(3,7)) 輸入描述:輸入包括一個整數n,(3 ≤ n < 1000)輸出描述:輸出對
java 輸入一個正整數求各個位數這和
package javaSE_第二週; import java.util.Scanner; /* *計算一個正整數求它各個位數之和 *問題:編寫一個程式,讓使用者輸入一個三位數的正整數值,然後計算該數各位數的和, *例如:如果輸入的數是123,則計算結果是6 *此方
Java執行緒的傳說(3)——如何關閉一個正在accept的ServerSocket?
加入一個ServerSocket正在另一個執行緒堵塞accept,那如何停止accept或者關閉Socket?Server socket 設定下超時 setSoTimeout 然後在Listen執行緒中用interrupt其實直接close socket也可以,不過會丟擲異
Java建立一個多執行緒的三種方式
步驟一:執行緒概念 首先要理解程序(Processor)和執行緒(Thread)的區別 程序:啟動一個LOL.exe就叫一個程序。 接著又啟動一個DOTA.exe,這叫兩個程序。 執行緒:執行緒是在程序內部同時做的事情,比如在LOL裡,有很多事情要同時做,比如"蓋倫” 擊殺“
JAVA定義一個執行緒池,迴圈遍歷list
文章目錄 前言 思路 下面是我自己專案中的呼叫程式碼,供你參考(ProcessNumTask就是那個實現Callable的任務): Callable與Future的介紹 Callable的介面定義如下:
JAVA 建立一個執行緒的三種方式
建立多執行緒-實現Runnable介面 建立類Battle,實現Runnable介面 啟動的時候,首先建立一個Battle物件,然後再根據該battle物件建立一個執行緒物件,並啟動 Battle battle1 = new Battle(gareen,teemo); new Thread(battle1
java建立一個執行緒的兩種方法及區別
第一種方法:繼承Thread類 public class NewThread extends Thread { public void run() { for(int i=0;i<20;i++) { System.out.println(i); } } }
dubbo如何關閉一個執行緒池的?
public static void gracefulShutdown(Executor executor, int timeout) { if (!(executor instanceof ExecutorService) || isShutdown(executor)
Java終止一個已啟動的執行緒
import java.math.BigInteger; import java.security.MessageDigest; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import
Java每天一個知識點+Demo-多執行緒相關命令
一 多執行緒相關命令介紹 1 wait() sleep() (1)這兩個方法來自不同的類分別是wait()來自Thread,sleep()來自Object。 (2) 最主要是sleep方法沒有釋放鎖,sleep使當前執行緒進入停滯狀態(阻塞當前執行緒),讓出cpu
Java停止一個執行緒的幾種方法
Java中停止一個執行緒有三種方法,分別是stop,interrupt和設定標誌位,我們依次來看一下這三種方法。 首先不推薦使用stop方法,原因有兩點: 1、原則上只要一呼叫thread.stop()方法,執行緒就會立即停止,並丟擲ThreadDeath error,查看
Java例項說明 100個執行緒同時向一個銀行賬戶中存入1元錢,在沒有使用同步機制和使用同步機制情況下的執行情況
銀行賬戶類: public class Account {private double balance; // 賬戶餘額public void deposit(double money) {double newBalance = balance + money;try {T
java-建立一個執行緒,在控制檯不斷輸出當前時間,精確到時分秒,每隔一秒輸出一次。
對其實現的一個方法是: 採用繼承Thread類 重寫run方法。 import java.text.SimpleDateFormat; import java.util.Date; /** * 建立一個執行緒,在控制檯不斷輸出當前時間,精確到時分秒,
如何關閉一個swingworker執行緒?
如果你在一個 SwingWorker 裡只用到了 doInBackground 方法 那和一個普通的 Thread 是沒有區別的 當你需要更新介面的時候,應該呼叫 publish 方法,通知 process 方法處理相關的介面更新 API 裡寫的很清楚 SwingWorker 的生命週期中包含三個執行緒:
php 中preg_replace執行一個正則表達式的搜索和替換
cnblogs param subject bject placement family sub ica ans preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $lim
Java 知識 - 集合、多執行緒、IO、JVM
GitHub 專案地址 Collection Java Collection 新增、刪除等操作時可選操作,如 Arrays.asList,會產生固定大小的集合,會丟擲 UnsupportedOperationException Set HashSet、TreeSet、LinkedH
【java併發程式設計實戰】—–執行緒基本概念
轉自 http://cmsblogs.com/?p=1638 共享和可變 要編寫執行緒安全的程式碼,其核心在於對共享的和可變的狀態進行訪問。 “共享”就意味著變數可以被多個執行緒同時訪問。我們知道系統中的資源是有限的,不同的執行緒對資源都是具有著同等的使用權。有限、公平就意味著競爭
java併發程式設計一一多執行緒之間通訊(一)
1.多執行緒之間如何實現通訊 多執行緒之間通訊,其實就是多個執行緒在操作同一個資源,但是操作的動作不同。 1.1什麼是多執行緒之間通訊? 需求:第一個執行緒寫入(input)使用者,另一個執行緒讀取(out)使用者。實現讀一個,寫一個操作。 1.2多執行緒之間通訊需求?