保存进度!
This commit is contained in:
parent
2e97a395c4
commit
28df267145
21
多线程编程/MultiThread/.vscode/launch.json
vendored
Normal file
21
多线程编程/MultiThread/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Current File",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "${file}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "App",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "com.cpic.xim.App",
|
||||||
|
"projectName": "MultiThread"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -11,17 +11,58 @@ package com.cpic.xim;
|
|||||||
|
|
||||||
import com.cpic.xim.utils.MyThread;
|
import com.cpic.xim.utils.MyThread;
|
||||||
import com.cpic.xim.utils.MyWorkingJob;
|
import com.cpic.xim.utils.MyWorkingJob;
|
||||||
|
import com.cpic.xim.thread.*;
|
||||||
|
|
||||||
public class App
|
public class App
|
||||||
{
|
{
|
||||||
public static void main( String[] args )
|
public static void main( String[] args )
|
||||||
{
|
{
|
||||||
MyThread workThread = new MyThread();
|
|
||||||
Thread jobThread = new Thread( new MyWorkingJob() );
|
// interrupteThread();
|
||||||
|
测试Stop线程();
|
||||||
workThread.start();
|
|
||||||
jobThread.start();
|
|
||||||
|
|
||||||
System.out.println( "我是主线程!" + String.valueOf( Thread.currentThread().threadId() ) );
|
System.out.println( "我是主线程!" + String.valueOf( Thread.currentThread().threadId() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void interrupteThread()
|
||||||
|
{
|
||||||
|
Runnable work = new InterruptedThread();
|
||||||
|
Thread workThread = new Thread( work );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
workThread.start();
|
||||||
|
Thread.sleep( 1000 );
|
||||||
|
workThread.interrupt();
|
||||||
|
}
|
||||||
|
catch ( InterruptedException error )
|
||||||
|
{
|
||||||
|
System.out.println( "线程异常" );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void 测试Stop线程()
|
||||||
|
{
|
||||||
|
Runnable work = new StopThread();
|
||||||
|
Thread workThread = new Thread( work );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
workThread.start();
|
||||||
|
Thread.sleep( 100 );
|
||||||
|
workThread.stop();
|
||||||
|
}
|
||||||
|
catch ( InterruptedException error )
|
||||||
|
{
|
||||||
|
System.out.println( "线程异常" );
|
||||||
|
}
|
||||||
|
catch ( ThreadDeath error )
|
||||||
|
{
|
||||||
|
System.out.println( "线程异常" );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2024-01-25 13:50:03
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @FilePath: /MultiThread/src/main/java/com/cpic/xim/thread/InterruptedThread.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2023} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.cpic.xim.thread;
|
||||||
|
|
||||||
|
public class InterruptedThread implements Runnable
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
System.out.println( "工作线程开始执行……" );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < 2000000; i++ )
|
||||||
|
{
|
||||||
|
if ( Thread.currentThread().isInterrupted() )
|
||||||
|
{
|
||||||
|
System.out.println( "该结束了……" );
|
||||||
|
|
||||||
|
throw new InterruptedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println( "工作线程计数:" + String.valueOf( i ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println( "工作线程结束!" );
|
||||||
|
}
|
||||||
|
catch ( InterruptedException error )
|
||||||
|
{
|
||||||
|
System.out.println( "工作线程中止!" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2024-01-25 15:52:34
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @FilePath: /MultiThread/src/main/java/com/cpic/xim/thread/StopThread.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2023} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
package com.cpic.xim.thread;
|
||||||
|
|
||||||
|
public class StopThread implements Runnable
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
System.out.println("开始stop测试,工作线程启动!");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for ( int i=0; i< 99999; i++)
|
||||||
|
{
|
||||||
|
// if ( Thread.currentThread().isInterrupted() )
|
||||||
|
// {
|
||||||
|
// throw new InterruptedException();
|
||||||
|
// }
|
||||||
|
|
||||||
|
System.out.println( "stop工作线程计数:" + String.valueOf( i ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println( "工作线程结束!" );
|
||||||
|
}
|
||||||
|
// catch ( InterruptedException error )
|
||||||
|
// {
|
||||||
|
// System.out.println("线程被 interrupte!!!!");
|
||||||
|
// }
|
||||||
|
catch ( ThreadDeath error )
|
||||||
|
{
|
||||||
|
System.out.println("线程被 stop!!!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user