提交 683f2827 编写于 作者: 武汉红喜's avatar 武汉红喜

java

上级 3b7bd399
......@@ -3,9 +3,7 @@ package org.hongxi.whatsmars.common;
import java.util.regex.Pattern;
/**
* Created on 2019/8/5.
*
* @author shenhongxi
* @author shenhongxi 2019/8/5
*/
public class Constants {
......
......@@ -5,6 +5,9 @@ import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Created by javahongxi on 2018/1/2.
*/
public class TCPClient {
public static void main(String[] args) {
......
......@@ -6,6 +6,9 @@ import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by javahongxi on 2018/1/2.
*/
public class TCPServer {
boolean started;
ServerSocket ss;
......
......@@ -7,6 +7,9 @@ import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* Created by javahongxi on 2018/1/2.
*/
public class UDPClient {
public static void main(String[] args) {
try {
......
......@@ -6,6 +6,9 @@ import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* Created by javahongxi on 2018/1/2.
*/
public class UDPServer {
public static void main(String[] args) {
try {
......
......@@ -4,6 +4,9 @@ import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* Created by javahongxi on 2018/1/2.
*/
public class MappedFileTest {
static private final int start = 0;
static private final int size = 1024;
......
......@@ -11,6 +11,9 @@ import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
/**
* Created by javahongxi on 2018/1/2.
*/
public class MultiPortEcho {
private int ports[];
private ByteBuffer echoBuffer = ByteBuffer.allocate(1024);
......
......@@ -9,6 +9,9 @@ import java.nio.charset.StandardCharsets;
import java.time.*;
import java.util.Base64;
/**
* Created by javahongxi on 2018/1/2.
*/
public class Java8Test {
@Test
......
......@@ -5,14 +5,13 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Created on 2019/8/8.
*
* @author shenhongxi
* @author shenhongxi 2019/8/8
*/
public class CallableTest {
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
// submit(Callable<T>)
Future<String> future = executorService.submit(() -> {
Thread.sleep(2000);
return "hello";
......
......@@ -11,6 +11,8 @@ public class CompletionServiceTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
CompletionService<Integer> completionService = new ExecutorCompletionService<>(executor);
// produce
for (int i = 0; i < 10; i++) {
final int seq = i + 1;
completionService.submit(() -> {
......@@ -18,6 +20,8 @@ public class CompletionServiceTest {
return seq;
});
}
// consume
for (int i = 0; i < 10; i++) {
try {
System.out.println(completionService.take().get());
......
......@@ -3,9 +3,7 @@ package org.hongxi.java.util.concurrent;
import java.util.concurrent.CountDownLatch;
/**
* Created on 2019/8/11.
*
* @author shenhongxi
* @author shenhongxi 2019/8/11
*
* @see JoinTest
* @see InvokeAllTest
......
......@@ -4,11 +4,14 @@ import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author shenhongxi 2019/8/11
*/
public class CyclicBarrierTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
final CyclicBarrier cb = new CyclicBarrier(3);
final CyclicBarrier barrier = new CyclicBarrier(3);
for (int i = 0; i < 3; i++) {
executorService.execute(() -> {
try {
......@@ -16,30 +19,31 @@ public class CyclicBarrierTest {
System.out.println("线程"
+ Thread.currentThread().getName()
+ "即将到达集合地点1,当前已有"
+ (cb.getNumberWaiting() + 1)
+ (barrier.getNumberWaiting() + 1)
+ "个已经到达,"
+ (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
+ (barrier.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
: "正在等候"));
cb.await();
barrier.await();
Thread.sleep((long) (Math.random() * 10000));
System.out.println("线程"
+ Thread.currentThread().getName()
+ "即将到达集合地点2,当前已有"
+ (cb.getNumberWaiting() + 1)
+ (barrier.getNumberWaiting() + 1)
+ "个已经到达,"
+ (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
+ (barrier.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
: "正在等候"));
cb.await();
barrier.await();
Thread.sleep((long) (Math.random() * 10000));
System.out.println("线程"
+ Thread.currentThread().getName()
+ "即将到达集合地点3,当前已有"
+ (cb.getNumberWaiting() + 1)
+ (barrier.getNumberWaiting() + 1)
+ "个已经到达,"
+ (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
+ (barrier.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
: "正在等候"));
cb.await();
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -4,6 +4,9 @@ import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author shenhongxi 2019/8/11
*/
public class ExchangerTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
......
......@@ -5,15 +5,14 @@ import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
/**
* Created on 2019/8/8.
*
* @author shenhongxi
* @author shenhongxi 2019/8/8
*/
public class FutureTaskTest {
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
FutureTask<String> task = new FutureTask<>(() -> "hello");
// submit(Runnable)
executorService.submit(task);
FutureTask<String> task2 = new FutureTask<>(() ->
System.out.println("..."), "world");
......
package org.hongxi.java.util.concurrent;
/**
* Created on 2019/8/10.
*
* @author shenhongxi
* @author shenhongxi 2019/8/10
*
* @see InterruptedTest
*/
......
package org.hongxi.java.util.concurrent;
/**
* Created on 2019/8/10.
*
* @author shenhongxi
* @author shenhongxi 2019/8/10
*/
public class InterruptedTest {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println(Thread.interrupted()); // currentThread().isInterrupted(true);
System.out.println(Thread.interrupted());
// currentThread().isInterrupted(true);
System.out.println(Thread.interrupted()); // true
System.out.println(Thread.interrupted()); // false
});
t.start();
t.interrupt(); // Just to set the interrupt flag
......
......@@ -6,9 +6,7 @@ import java.util.List;
import java.util.concurrent.*;
/**
* Created on 2019/8/11.
*
* @author shenhongxi
* @author shenhongxi 2019/8/11
*/
public class InvokeAllTest {
......
......@@ -4,6 +4,9 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author shenhongxi 2019/8/11
*/
public class ReentrantLockTest {
public static void main(String[] args) {
......
......@@ -6,9 +6,7 @@ import java.util.concurrent.Future;
import java.util.stream.IntStream;
/**
* Created on 2019/8/8.
*
* @author shenhongxi
* @author shenhongxi 2019/8/8
*
* @see CallableTest
* @see FutureTaskTest
......
......@@ -7,9 +7,7 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* Created on 2019/8/10.
*
* @author shenhongxi
* @author shenhongxi 2019/8/10
*/
public class ScheduleTest {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册