提交 4cfec701 编写于 作者: E ex_kongxiang

feat : 工具类测试

上级 d3e5903d
import org.testng.annotations.Test;
import java.util.concurrent.CountDownLatch;
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class CountDownLatchTest {
@Test
public void test() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(5);
SimpleThreadUtils.newLoopThread(5, "cd", 1, (t) -> {
int i = t.getNo() * 1000;
try {
Thread.sleep(i);
} catch (InterruptedException e) {
}
System.out.println("加载资源线程:"+t.getName()+": 花费了 " + i + "s");
countDownLatch.countDown();
});
countDownLatch.await();
System.out.println("开始游戏");
}
}
import org.testng.annotations.Test;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class CyclicBarrierTest {
@Test
public void test() throws InterruptedException {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
SimpleThreadUtils.newLoopThread(5, "cd", 1, (t) -> {
while (true) {
int i = t.getNo() * 1000;
try {
Thread.sleep(i);
} catch (InterruptedException e) {
}
System.out.println(":" + t.getName() + ": 准备好了");
try {
cyclicBarrier.await();
System.out.println(" 冲。。。 :" + t.getName());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (BrokenBarrierException e) {
throw new RuntimeException(e);
}
}
});
SimpleThreadUtils.wait(10000,()->{});
}
}
import org.testng.annotations.Test;
import java.util.concurrent.Exchanger;
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class ExchangerTest {
@Test
public void test() {
Exchanger<String> exchanger = new Exchanger<>();
SimpleThreadUtils.newLoopThread(2,"ex",2,(t)->{
if (t.getNo() == 0){
try {
String exchange = exchanger.exchange("我先到了,你死定了");
System.out.println(t.getName()+":"+exchange);
} catch (InterruptedException e) {
}
}else {
try {
Thread.sleep(5000);
String exchange = exchanger.exchange("先到了吧,去死吧");
System.out.println(t.getName()+":"+exchange);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
SimpleThreadUtils.wait(1000,()->{});
}
}
import org.testng.annotations.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
/**
* @author 孔翔
* @since 2023-10-25
* copyright for author : 孔翔 at 2023-10-25
* java-study
*/
public class SemaphoreTest {
@Test
public void test() throws InterruptedException {
Semaphore semaphore = new Semaphore(5);
SimpleThreadUtils.newLoopThread(10, "cd", 10, (t) -> {
try {
semaphore.acquire();
int i = t.getNo() * 1000;
try {
Thread.sleep(i);
} catch (InterruptedException e) {
}
System.out.println("加载资源线程:"+t.getName()+": 花费了 " + i + "s");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}finally {
semaphore.release();
}
});
SimpleThreadUtils.wait(10000,()->{});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册