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

java

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