C#で中止
Abort()メソッドは、スレッドを破棄するために使用されます。
ランタイムは、ThreadAbortExceptionをスローしてスレッドを中止します。この例外をキャッチすることはできません。コントロールはfinallyブロックに送信されます。
スレッドでAbort()メソッドを使用する-
childThread.Abort();
例
using System;
using System.Threading;
namespace MultithreadingApplication {
class ThreadCreationProgram {
public static void CallToChildThread() {
try {
Console.WriteLine("Child thread starts");
// do some work, like counting to 10
for (int counter = 0; counter <= 10; counter++) {
Thread.Sleep(500);
Console.WriteLine(counter);
}
Console.WriteLine("Child Thread Completed");
} catch (ThreadAbortException e) {
Console.WriteLine("Thread Abort Exception");
} finally {
Console.WriteLine("Couldn't catch the Thread Exception");
}
}
static void Main(string[] args) {
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
//stop the main thread for some time
Thread.Sleep(5000);
//now abort the child
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort();
Console.ReadKey();
}
}
} 出力
In Main: Creating the Child thread Child thread starts 0 1 2 3 4 5 6 7 8 In Main: Aborting the Child thread Thread Abort Exception Couldn't catch the Thread Exception
-
スレッドの優先度を表示するC#プログラム
C#でスレッドの優先度を表示するには、優先度を使用します プロパティ。 まず、 currentThreadを使用します スレッドに関する情報を表示するプロパティ- Thread thread = Thread.CurrentThread; ここで、 thread.Priorityを使用します スレッドの優先度を表示するプロパティ- thread.Priority 例 C#でスレッドの優先度を示す完全なコードを見てみましょう。 using System; using System.Threading; namespace Demo { class MyClass
-
Javaのスレッドプール
スレッドプールは、事前に初期化されたスレッドのコレクションです。スレッドプールの背後にある一般的な計画は、メソッドの起動時にさまざまなスレッドを形成し、それらが座って作業を期待する場所に配置することです。サーバーは参加の呼び出しを受信すると、このプールからスレッドを起動し(使用可能な場合)、サービスの要求を渡します。スレッドがサービスを完了すると、プールに戻り、多くの作業を待ちます。プールにアクセス可能なスレッドが含まれていない場合、サーバーはスレッドが解放されるまで待機します。 新しいスレッドを作成する必要がないため、時間を節約できます。 サーブレットとJSPで、リクエストをメソッド化す