提交 771a3428 编写于 作者: E ex_kongxiang

feat: synchronized 代码实例

上级 1bbe9142
out/
*.iml
*.xml
\ No newline at end of file
/**
* @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();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册