- 1. 空中网面试题1
-
- package com.kongzhongwang.interview;
-
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
-
-
-
-
-
-
-
-
-
- public class ReadLog {
- public static void main(String[] args) {
-
-
-
-
- final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);
- for (int i = 0; i < 4; i++) {
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- try {
- String log = queue.take();
- parseLog(log);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- System.out.println("begin:" + (System.currentTimeMillis() / 1000));
-
-
-
-
- for (int i = 0; i < 16; i++) {
- final String log = "" + (i + 1);
- {
-
- try {
- queue.put(log);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
-
- public static void parseLog(String log) {
- System.out.println(log + ":" + System.currentTimeMillis() / 1000);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- 2. 空中网面试题2
-
- package com.kongzhongwang.interview;
-
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.SynchronousQueue;
-
-
-
-
-
-
-
- public class Test {
-
- public static void main(String[] args) {
-
-
- final Semaphore semaphore = new Semaphore(1);
-
- final SynchronousQueue<String> queue = new SynchronousQueue<String>();
- for(int i=0;i<10;i++){
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- try {
- semaphore.acquire();
- String input = queue.take();
- String output = TestDo.doSome(input);
- System.out.println(Thread.currentThread().getName() + ":" + output);
- semaphore.release();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- System.out.println("begin:" + (System.currentTimeMillis() / 1000));
-
- for (int i = 0; i < 10; i++) {
- String input = i + "";
- try {
- queue.put(input);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
-
- class TestDo {
-
- public static String doSome(String input) {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- String output = input + ":" + (System.currentTimeMillis() / 1000);
- return output;
- }
-
- }
- 3.空中网面试题3
-
- package com.kongzhongwang.interview;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.concurrent.CopyOnWriteArrayList;
-
- public class Tests extends Thread {
-
-
-
-
-
-
-
-
-
-
-
-
- private TestsDo testDo;
- private String key;
- private String value;
-
- private Tests(String key, String key2, String value) {
- this.testDo = TestsDo.getInstance();
-
-
-
-
- this.key = key + key2;
-
-
-
-
-
-
-
- this.value = value;
- }
-
- public static void main(String[] args) {
-
- Tests a = new Tests("1", "", "1");
- Tests b = new Tests("1", "", "2");
- Tests c = new Tests("3", "", "3");
- Tests d = new Tests("4", "", "4");
- System.out.println("begin:" + (System.currentTimeMillis() / 1000));
- a.start();
- b.start();
- c.start();
- d.start();
- }
-
- public void run() {
- testDo.doSome(key, value);
- }
- }
-
- class TestsDo {
- private TestsDo() {}
- private static TestsDo _instance = new TestsDo();
- public static TestsDo getInstance() {
- return _instance;
- }
-
-
- private CopyOnWriteArrayList keys = new CopyOnWriteArrayList();
- public void doSome(Object key,String value){
- Object o = key;
- if(! keys.contains(o)){
- keys.add(o);
- }else{
-
- for(Iterator iter = keys.iterator();iter.hasNext();){
-
-
-
-
- try {
- Thread.sleep(20);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Object oo = iter.next();
- if(o.equals(oo)){
- o = oo;
- }
- }
- }
-
- synchronized(o)
-
- {
- try{
- Thread.sleep(1000);
- System.out.println(key+":"+value+":" + (System.currentTimeMillis() / 1000));
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
空中网面试题
原文:http://www.cnblogs.com/chenchangyan/p/3539677.html