代码拉取完成,页面将自动刷新
同步操作将从 ldh123/TestUI 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package util;
import java.util.concurrent.Exchanger;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by ldh on 2019/4/1.
*/
public class ExchangeTest {
public static void main(String[] args) {
testExchanger();
}
public static void testExchanger() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Exchanger<Integer> exchanger = new Exchanger();
executor.execute(()->{
for (int i=0; i<100; i+=2) {
try {
int r = exchanger.exchange(i);
System.out.println("thread1: " + i + ", r:" + r);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executor.execute(()->{
for (int i=1; i<100; i+=2) {
try {
int r = exchanger.exchange(10);
// Thread.sleep(1);
System.out.println("thread2: " + i + ", r:" + r);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executor.shutdown();
}
public void commonTest() {
ExecutorService executor = Executors.newFixedThreadPool(2);
ExchangeTest exchangeTest = new ExchangeTest();
executor.execute(()->{
for (int i=0; i<100; i+=2) {
try {
exchangeTest.println("thread1", i);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executor.execute(()->{
for (int i=1; i<100; i+=2) {
try {
exchangeTest.println2("thread2", i);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executor.shutdown();
}
private boolean printlnThread = true;
public synchronized void println(String threadName, int n) throws InterruptedException {
if (!printlnThread) {
wait();
}
System.out.println(threadName + ":" + n);
printlnThread = false;
notifyAll();
}
public synchronized void println2(String threadName, int n) throws InterruptedException {
if (printlnThread) {
wait();
}
System.out.println(threadName + ":" + n);
printlnThread = true;
notifyAll();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。