保存进度!

This commit is contained in:
Kane Wang 2024-01-25 18:01:13 +08:00
parent 2e97a395c4
commit 28df267145
4 changed files with 150 additions and 5 deletions

View 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"
}
]
}

View File

@ -11,17 +11,58 @@ package com.cpic.xim;
import com.cpic.xim.utils.MyThread;
import com.cpic.xim.utils.MyWorkingJob;
import com.cpic.xim.thread.*;
public class App
{
public static void main( String[] args )
{
MyThread workThread = new MyThread();
Thread jobThread = new Thread( new MyWorkingJob() );
workThread.start();
jobThread.start();
// interrupteThread();
测试Stop线程();
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( "线程异常" );
}
}
}

View File

@ -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( "工作线程中止!" );
}
}
}

View File

@ -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");
}
}
}