面试集锦

来源:互联网 发布:东北林大网络教学平台 编辑:程序博客网 时间:2024/05/16 10:43
package com.h.test;import java.util.HashMap;import java.util.Map;/** * Created by John on 2017/8/26. */public class Person {    private String name;    @Override    public int hashCode() {        return name.hashCode();    }    public void setName(String name) {        this.name = name;    }    public static void main(String[] args) {        Map<Person,String> map = new HashMap<>();        Person p = new Person();        p.setName("A");        map.put(p,"X");        p.setName("B");        map.put(p,"Y");        /**         * 因为Person类中重写了hashCode(),依赖String类的hashCode()         * 所以两次put操作,虽然操作的是同一个Person实例,但因为中间改变了         * name成员变量值,导致在第二次put()时p的hashCode()返回值被改变,         * 会被放入Entry[]中的另外一个位置.在get(p)操作时会根据p的hashCode()去对应的         * 数组位置处取值,因为此时p的name=“B”,所以取的是第二次的值,但Entry[]数组中存的是         * 有2个Entry,即使2个Entry的key是同一个类实例,但在HashMap看来,你的hashCode()不一样,         * 你两个key就不一样,这也从侧面说明了正确的重写hashCode()的重要性。         */        System.out.println(map.get(p));//Y        System.out.println(map.size());//2    }}

请使用Java提供的synchronized块、wait()、notify()和notifyAll()方法,实现三个并发执行的线程1、2和3协同输出ABCABC…ABC这样的字符串序列(序列中ABC重复出现10次),其中线程1负责打印输出字母A,线程2负责打印输出字母B,线程3负责打印输出字母C。

一.wait()/notify()

package com.h.thread;/** * Created by John on 2017/8/30. * 使用wait()...notify()实现线程间通信 * 需求:循环输出ABC...10次 */public class WaitNotifyTest {    public static void main(String[] args) throws InterruptedException {        Object lockA = new Object();        Object lockB = new Object();        Object lockC = new Object();        PrintTask taskA = new PrintTask("A",lockC,lockA);        PrintTask taskB = new PrintTask("B",lockA,lockB);        PrintTask taskC = new PrintTask("C",lockB,lockC);        //这里的启动顺序不能变        new Thread(taskA).start();        Thread.sleep(1000);        new Thread(taskB).start();        Thread.sleep(1000);        new Thread(taskC).start();    }}class PrintTask implements Runnable{    private String name;    private Object prev;    private Object self;    public PrintTask(String name, Object prev, Object self) {        this.name = name;        this.prev = prev;        this.self = self;    }    @Override    public void run() {        int count = 10;        while (count > 0) {            synchronized (prev){                synchronized (self){                    System.out.print(name);                    count--;                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    self.notify();                }                try {                    prev.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}
package com.h.thread;/** * Created by John on 2017/8/30. * 使用wait()...notify()实现线程间通信 * 需求:循环输出ABC...10次 */public class WaitNotifyTest {    public static void main(String[] args) throws InterruptedException {        Object lockA = new Object();        Object lockB = new Object();        Object lockC = new Object();        new Thread(new PrintTask(lockC,lockA),"A").start();        Thread.sleep(1000);        new Thread(new PrintTask(lockA,lockB),"B").start();        Thread.sleep(1000);        new Thread(new PrintTask(lockB,lockC),"C").start();    }}class PrintTask implements Runnable{    private Object prev;    private Object self;    public PrintTask(Object prev, Object self) {        this.prev = prev;        this.self = self;    }    @Override    public void run() {        for (int i=0;i<10;i++){            synchronized (prev){                synchronized (self){                    System.out.print(Thread.currentThread().getName());                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    self.notify();                }                try {                    prev.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}

二.Lock

package com.h.thread;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * Created by John on 2017/8/30. */public class Test {    private static volatile int cnt = 0;    static Lock lk = new ReentrantLock();    public static void main(String[] args) throws InterruptedException {        Thread A = new Thread(new Runnable() {            @Override            public void run() {                while(cnt <= 30) {                    lk.lock();                    if (cnt % 3 == 0) {                        System.out.print("A ");                        cnt++;                    }                    lk.unlock();                }            }        });        Thread B = new Thread(new Runnable() {            @Override            public void run() {                while(cnt <= 30) {                    lk.lock();                    if (cnt % 3 == 1) {                        System.out.print("B ");                        cnt++;                    }                    lk.unlock();                }            }        });        Thread C = new Thread(new Runnable() {            @Override            public void run() {                while(cnt <= 30) {                    lk.lock();                    if (cnt % 3 == 2) {                        System.out.print("C\n");                        cnt++;                    }                    lk.unlock();                }            }        });        /**         * 这里的启动先后顺序无所谓,因为run()中对线程间的共享变量cnt做了输出条件控制         */        B.start();        A.start();        C.start();    }}
原创粉丝点击