面试题:要求用线程顺序打印A1B2C3....Z26

 

1 用synchronized+wait()+notify()实现

 

public class T_Sync_wait_notify {

 

    public static void main(String[] args) {

        final Object o = new Object();

 

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        new Thread(() -> {

 

            synchronized (o) {

                for (char c : aI) {

                    System.out.print(c);

                    try {

                        o.notify();

                        o.wait(); //让出锁

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

                o.notify(); //必须,否则无法停止程序

            }

        },"t1").start();

 

        new Thread(()->{

 

            synchronized (o){

                for (char c:aC){

 

                    System.out.print(c);

                    try {

                        o.notify();

                        o.wait();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

                o.notify();

            }

 

        },"t2").start();

    }

 

}

 

2 用LockSupport的park()和unPark()实现

 

public class T_LockSupport {

 

    static Thread t1 = null, t2 = null;

 

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        t1 = new Thread(() -> {

            for (char c : aI) {

                System.out.print(c);

                LockSupport.unpark(t2); //叫醒T2

                LockSupport.park();//T1阻塞

            }

        }, "t1");

 

        t2 = new Thread(() -> {

 

            for (char c : aC) {

                LockSupport.park(); //t2阻塞

                System.out.print(c);

                LockSupport.unpark(t1);//叫醒t1

            }

 

        });

 

        t1.start();

        t2.start();

    }

}

 

3 ReentrantLock+1个Condition实现

 

 

 

 

 

public class T_Lock_Condition {

 

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        Lock lock = new ReentrantLock();

        Condition condition = lock.newCondition();

 

        new Thread(() -> {

            try {

                lock.lock();

                for (char c : aI) {

                    System.out.print(c);

                    condition.signal();

                    condition.await();

                }

                condition.signal();

            } catch (InterruptedException e) {

                e.printStackTrace();

            } finally {

                lock.unlock();

            }

        }, "t1").start();

 

 

        new Thread(() -> {

            try {

                lock.lock();

                for (char c : aC) {

                    System.out.print(c);

                    condition.signal();

                    condition.await();

                }

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                lock.unlock();

            }

 

        }, "t2").start();

    }

}

 

4 ReentrantLock+2个Condition实现

 

public class T_Lock_Condition2 {

 

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        Lock lock = new ReentrantLock();

        Condition condition1 = lock.newCondition();

        Condition condition2 = lock.newCondition();

 

        new Thread(() -> {

 

 

            try {

                lock.lock();

                for (char c : aI) {

                    System.out.print(c);

                    condition2.signal();

                    condition1.await();

                }

                condition2.signal();

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                lock.unlock();

            }

 

        }, "t1").start();

 

 

        new Thread(() -> {

 

            try {

                lock.lock();

                for (char c : aC) {

                    System.out.print(c);

                    condition1.signal();

                    condition2.await();

                }

                condition1.signal();

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                lock.unlock();

            }

 

        }).start();

 

    }

}

 

5 AtomicInteger实现

 

public class T_AtomicInteger {

 

    static AtomicInteger threadNo = new AtomicInteger(1);

 

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        new Thread(() -> {

            for (char c : aI) {

                while (threadNo.get() != 1) {

                }

                System.out.print(c);

                threadNo.set(2);

            }

        }, "t1").start();

 

        new Thread(() -> {

 

            for (char c : aC) {

 

                while (threadNo.get() != 2) {

                }

                System.out.print(c);

                threadNo.set(1);

 

            }

 

        }).start();

 

 

    }

}

 

6 ArrayBlockingQueue实现

public class T_BlockingQueue {

 

    static BlockingQueue<String> q1 = new ArrayBlockingQueue<>(1);

    static BlockingQueue<String> q2 = new ArrayBlockingQueue<>(1);

 

    public static void main(String[] args) {

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        new Thread(() -> {

 

            for (char c : aI) {

                System.out.print(c);

                try {

                    q1.put("ok");

                    q2.take();

                } catch (Exception e) {

                    e.printStackTrace();

                }

 

            }

 

        }, "t1").start();

 

 

        new Thread(() -> {

 

            for (char c : aC) {

 

                try {

                    q1.take();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                System.out.print(c);

 

                try {

                    q2.put("ok");

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

 

            }

 

        }, "t2").start();

 

    }

 

}

7 用LinkedTransferQueue实现

 

public class T_TransferQueue {

 

    public static void main(String[] args) {

 

 

        char[] aI = "1234567".toCharArray();

        char[] aC = "ABCDEFG".toCharArray();

 

        TransferQueue<Character> queue = new LinkedTransferQueue<>();

 

        new Thread(() -> {

 

 

            for (char c : aI) {

                try {

                    System.out.print(queue.take());

                    queue.transfer(c);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

 

 

        },"t1").start();

 

 

        new Thread(()->{

 

            for (char c:aC){

 

                try {

                    queue.transfer(c);

                    System.out.print(queue.take());

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

 

            }

 

        },"t2").start();

 

    }

}

实现方法有很多种,这里就不一一列举了......

猜你喜欢

转载自blog.csdn.net/huzhiliayanghao/article/details/106794118