1. Air Network test 1 package com.kongzhongwang.interview;
Import Java.util.concurrent.ArrayBlockingQueue;
Import Java.util.concurrent.BlockingQueue;
MyEclipse Powerful features: Copying the code directly to the SRC path of the project automatically generates the package name and class name/** * * Air mesh Test 1: The existing program code simulation produces 16 log objects, and requires 16 seconds to run to print these logs, add four threads to the program to call
* Parselog () method to print the 16 log objects separately, the program can only run 4 seconds to print out these log objects. * Review of new technology Blockingqueue/public class Readlog {public static void main (string[] args) {/* Here's a coincidence: here Arrayblockingque Ue<string> (1) and arrayblockingqueue<string> (16) * Achieve the same effect, and the former produces a more orderly data combination; At present, it is a coincidence to expect Daniel to find cause and effect to inform the final B
lockingqueue<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)); /*
* The simulation handles 16 logs, the following code produces 16 log objects, and the current code needs to run 16 seconds to print these log objects; * Modify the program code, open four threads for 16 objects in 4 seconds after the * * for (int i = 0; i < i++) {//This line of code cannot change final String log = "" + (i + 1);
This line of code cannot change {//Readlog.parselog (log);
try {queue.put (log);
catch (Interruptedexception e) {e.printstacktrace (); "}}//Parselog internal code cannot move public static void Parselog (String log) {System.out.println (log +": "+ System.curr
Enttimemillis ()/1000);
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace (); }} 2.
2 package Com.kongzhongwang.interview of the aerial network test;
Import Java.util.concurrent.Semaphore;
Import Java.util.concurrent.SynchronousQueue; /** * Air Mesh Test 2: The code in the test class in the ready-made program is constantly generating data and then handing it to the Testdo.dosome () method; * It's as if producers are constantly generating data and consumers are constantly consuming data. Please change the program to 10 threads to produce data, * These consumers call the Testdo.dosome () method to deal with, solid each consumer needs 1 seconds to finish, the program should ensure that these * consumer threads sequentially ordered consumption data, only the last consumer after consumption,
The next consumer will be able to consume data, and the next consumer can be anyone, but make sure that the data that the consumer gets is in order. */public class Test {public statIC void Main (string[] args) {//using the semaphore semaphore equivalent to the last lock lock final semaphore semaphore = new semaphore (1);
New Queue mode final synchronousqueue<string> queue = synchronousqueue<string> (); for (int i=0;i<10;i++) {new Thread (new Runnable () {@Override the public void run () {try {Semap
Hore.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 < i++) {//This line of code cannot change String input = i + "";
This line of code cannot change try {queue.put (input);
catch (Interruptedexception e) {e.printstacktrace ();
Cannot move class Testdo {public static string Dosome (String input) {try {thread.sleep (1000)}}//Testdo classes; } catch (Exception e) {e.printstacktrace ();
String output = input + ":" + (System.currenttimemillis ()/1000);
return output;
} 3. Aerial Network test 3 package Com.kongzhongwang.interview;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.concurrent.CopyOnWriteArrayList; public class Tests extends thread {/** * Aerial network question 3: The existing program simultaneously initiates four threads to invoke the Testdo.dosome (Key,value) method; * Due to Testsdo.dosome (ke Y,value) The code within the method is to pause for 1 seconds before outputting the current time value in seconds, * so it prints out four identical time values, as follows: 4:4 1258199615 1:1 1258199615 3:3 1258199615 2:2 * 125819 9615; Modify the code, if several threads call the Testdo.dosome (Key,value) method; * The key value passed in is equal (equals is true), then these lines thread mutually exclusive outputs, that is, when the key of two threads is 1 o'clock, * One of them will output the result later than the other threads, as follows: 4:4 1258199615 1:1 1258199615 3:3 1258199615 1:2 * 1258199616; In short, the key specified in each thread is equal; These equal lines thread every 1 seconds
Time value (to use mutual exclusion), * key is different, then the parallel implementation (mutual exclusion)/private testsdo testdo;
Private String key;
private String value;
Private Tests (String key, String Key2, String value) {this.testdo = Testsdo.getinstance (); * * Constant "1 "and" 1 "are the same object, the following line of code is to use" 1 "+" "way to generate new objects; * To achieve content unchanged, still equal (all also" 1 "), but the object is no longer the same effect * * This.key = key + Key2;
* * a = "1" + "";
* b = "2" + ""; * A and B are the same objects, because the compiler optimizes it to a= "1" before it is executed; * but This.key = key + Key2; The compiler will not give you an optimization, * because you are a property variable, the compiler doesn't know what value you will pass in the future.
. 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;
//traditional writing, not taking into account thread concurrency issues//private ArrayList keys = new ArrayList ();
Private copyonwritearraylist keys = new Copyonwritearraylist (); public void Dosome (Object key,string value) {
Object o = key;
if (! Keys.contains (o)) {keys.add (o); No other actions can be performed during}else{//iterative process; for (iterator iter = Keys.iterator (); Iter.hasnext ();) {//* Here's hibernation: In order for everyone to see, using the traditional private Ar
raylist keys = new ArrayList (); * Causes exception in thread "Thread-1" Java.util.ConcurrentModificationException exception
* Because there is no other operation in the iterative process, you have to add data to it during the iteration to cause this exception, and in the iteration to put sleep this error.
* * try {thread.sleep (20);
catch (Interruptedexception e) {e.printstacktrace ();
Object oo = Iter.next ();
if (O.equals (oo)) {o = oo; Here for the difference is different object, so can not directly use synchronized (key) synchronized (O)//curly braces is required to synchronize the code, can not change the {try{thread.sleep
(1000);
System.out.println (key+ ":" +value+ ":" + (System.currenttimemillis ()/1000));
}catch (Exception e) {e.printstacktrace ();
}
}
}
}