<file_sep>package lock;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {

    @Test
    public void testCountDownLatch() throws InterruptedException {

        final CountDownLatch latch = new CountDownLatch(2);

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("request agreement service");
            latch.countDown();
        }).start();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("request member service");
            latch.countDown();
        }).start();

        latch.await();
        System.out.println("merge response");
    }
}
<file_sep>count from 1 to 1000000 by creating 10 threads
which class is the fastest, SynchronizedAccumulator, AtomicAccumulator or AtomicAdderAccumulator
find the testing result in AccumulatorTest
                                               <file_sep>ThreadSafeCounter is a counter providing synchronized increase and decrease methods by using synchronized
ThreadSafeList is a container providing synchronized put and get methods by using ReentrantLock and Condition <file_sep>package interview.crossprint;

import org.junit.Test;

import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

public class LockSupportTest {

    private Thread oddThread = null;
    private Thread evenThread = null;

    @Test
    public void testCrossPrint() throws InterruptedException {

        CountDownLatch latch = new CountDownLatch(1);

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 20; i++) {
            stack.push(20 - i);
        }

        oddThread = new Thread(() -> {
            System.out.println("odd thread starts");
            while (!stack.isEmpty()) {
                System.out.println("odd thread prints " + stack.pop());
                LockSupport.unpark(evenThread);
                LockSupport.park();
            }
        }, "odd");

        evenThread = new Thread(() -> {
            System.out.println("even thread starts");
            latch.countDown();
            while (!stack.isEmpty()) {
                LockSupport.park();
                System.out.println("even thread prints " + stack.pop());
                LockSupport.unpark(oddThread);
            }
        }, "even");

        evenThread.start();
        latch.await();
        oddThread.start();

        oddThread.join();
        evenThread.join();
    }
}
<file_sep>package reference;

/**
 * @author eric
 */
public class ReferenceModel {

    /**
     * override finalize is not recommended, here just for test
     *
     * @throws Throwable
     */
    @Override
    protected void finalize() throws Throwable {
        System.out.println(this.getClass().getName() + " is finalized");
    }
}
<file_sep>package threadpool;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class FutureTaskTest {

    @Test
    public void testFutureTask() throws ExecutionException, InterruptedException {

        ExecutorService service = Executors.newFixedThreadPool(1);

        FutureTask<String> task = new FutureTask<>(() -> {
            return "hello future task";
        });
        service.submit(task);

        System.out.println(task.get());

        service.shutdown();
    }
}
<file_sep>package cas;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author eric
 */
public class AtomicAccumulator {

    public static AtomicLong count = new AtomicLong(0);

    public void increase(){
        count.incrementAndGet();
    }
}
<file_sep>This package is to show how volatile and synchronized block code work<file_sep>This package is to show how to create and start a thread<file_sep>package thread;

/**
 * @author eric
 */
public class SleepThread extends Thread{
    @Override
    public void run() {
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("this is a sleep thread extending Thread");
    }
}
<file_sep>package interview.crossprint;

import org.junit.Test;

import java.util.Stack;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.locks.LockSupport;

public class LinkedTransferQueueTest {

    @Test
    public void testCrossPrint() throws InterruptedException {

        LinkedTransferQueue<Integer> queue = new LinkedTransferQueue<>();

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 20; i++) {
            stack.push(20 - i);
        }

        final Thread oddThread = new Thread(() -> {
            System.out.println("odd thread starts");
            while (!stack.isEmpty()) {
                try {
                    System.out.println("odd thread prints " + queue.take());
                    queue.transfer(stack.pop());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "odd");

        final Thread evenThread = new Thread(() -> {
            System.out.println("even thread starts");
            while (!stack.isEmpty()) {
                try {
                    queue.transfer(stack.pop());
                    System.out.println("even thread prints " + queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "even");

        oddThread.start();
        evenThread.start();

        oddThread.join();
        oddThread.join();
    }
}
<file_sep>package threadpool;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureTest {

    @Test
    public void testCallable() throws ExecutionException, InterruptedException {

        ExecutorService service = Executors.newFixedThreadPool(1);

        // callable
        final Callable<Integer> integerCallable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 1;
            }
        };
        final Future<Integer> integerFuture = service.submit(integerCallable);

        // lambada
        final Future<String> stringFuture = service.submit(() -> {
            return "hello future";
        });

        System.out.println(integerFuture.get());
        System.out.println(stringFuture.get());

        service.shutdown();
    }
}
