...
 
Commits (2)
    https://gitcode.net/k316378085/java-study/-/commit/1bbe914278ce320d6fcb051de51b8b939c2dc8af 更新 README 2023-10-23T18:04:32+08:00 ex_kongxiang ex_kongxiang@partner.midea.com https://gitcode.net/k316378085/java-study/-/commit/771a34283fb8b82737e00cbf68d8839dc2d7c503 feat: synchronized 代码实例 2023-10-25T11:42:23+08:00 ex_kongxiang ex_kongxiang@partner.midea.com
out/
*.iml
*.xml
\ No newline at end of file
# 默认忽略的文件
/shelf/
/workspace.xml
JAVA Study过程代码 JAVA Study过程代码
- 无 Maven 依赖管理
- 只依赖JAVA环境
- IDEA直接运行
JAVAC 编译
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class SynchronizedLock {
/**
* 共享变量
*/
private int value;
public void update(int value) {
int sum = this.value + value;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
this.value = sum;
}
public synchronized void updateSafe(int value) {
int sum = this.value + value;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
this.value = sum;
}
public int getValue() {
return value;
}
}
import org.testng.annotations.Test;
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class SynchronizedLockTest {
SynchronizedLock synchronizedLock = new SynchronizedLock();
@Test
public void testUpdate() {
new Thread(()->{
int i = 0;
while (i++ < 10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronizedLock.updateSafe(100);
System.out.println(Thread.currentThread().getName()+":"+synchronizedLock.getValue());
}
}).start();
new Thread(()->{
int i = 0;
while (i++ < 10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronizedLock.updateSafe(-80);
System.out.println(Thread.currentThread().getName()+":"+synchronizedLock.getValue());
}
}).start();
while (true){
try {
Thread.sleep(1000);
System.out.println("监控: "+synchronizedLock.getValue());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file
/**
* @author 孔翔
* @since 2023-10-23
* copyright for author : 孔翔 at 2023-10-23
* java-study
*/
public class AThread extends Thread {
@Override
public void run() {
System.out.println("hello : 我是A Thread");
}
}
import java.util.concurrent.Callable;
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "Hello, Callable!";
}
}
/**
* @author 孔翔
* @since 2023-10-23
* copyright for author : 孔翔 at 2023-10-23
* java-study
*/
public class RunnableThread implements Runnable{
@Override
public void run() {
System.out.println("Runnable 实现: 线程 Runnable");
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author 孔翔
* @since 2023-10-23
* copyright for author : 孔翔 at 2023-10-23
* java-study
*/
public class ThreadCreate {
public static void main(String[] args) {
// runnable
new Thread(new RunnableThread()).start();
// extends Thread
AThread aThread = new AThread();
aThread.start();
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new MyCallable());
try {
String result = future.get();
System.out.println("Callable result: " + result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
executor.shutdown();
}
}
}