---------------------- android培訓、java培訓、期待與您交流! ----------------------
張老師講解答案:
1.
public class Test {
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(){
@Override
public void run() {
while(true){
try {
String log = queue.take();
parseLog(log);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
System.out.println("begin:"+(System.currentTimeMillis()/1000));
/*模擬處理16行日誌,下面的代碼產生了16個日誌對象,當前代碼需要運行16秒才能打印完這些日誌。
修改程序代碼,開四個線程讓這16個對象在4秒鐘打完。
*/
for(int i=0;i<16;i++){ //這行代碼不能改動
final String log = ""+(i+1);//這行代碼不能改動
{
try {
queue.put(log);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Test.parseLog(log);
}
}
}
//parseLog方法內部的代碼不能改動
public static void parseLog(String log){
System.out.println(log+":"+(System.currentTimeMillis()/1000));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.
public class Test1 {
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) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//不能改動此TestDo類
class TestDo {
public static String doSome(String input){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String output = input + ":"+ (System.currentTimeMillis() / 1000);
return output;
}
}
3.
//不能改動此Test類
public class Test extends Thread{
private TestDo testDo;
private String key;
private String value;
public Test(String key,String key2,String value){
this.testDo = TestDo.getInstance();
/*常量"1"和"1"是同一個對象,下面這行代碼就是要用"1"+""的方式產生新的對象,
以實現內容沒有改變,仍然相等(都還爲"1"),但對象卻不再是同一個的效果*/
this.key = key+key2;
/*a = "1"+""; a b爲同一對象
b = "1"+""
*/
this.value = value;
}
public static void main(String[] args) throws InterruptedException{
Test a = new Test("1","","1");
Test b = new Test("1","","2");
Test c = new Test("3","","3");
Test d = new Test("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 TestDo {
private TestDo() {}
private static TestDo _instance = new TestDo();
public static TestDo getInstance() {
return _instance;
}
//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);
}else{
for(Iterator iter=keys.iterator();iter.hasNext();){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object oo = iter.next();
if(oo.equals(o)){
o = oo;
break;
}
}
}
synchronized(o)
// 以大括號內的是需要局部同步的代碼,不能改動!
{
try {
Thread.sleep(1000);
System.out.println(key+":"+value + ":"
+ (System.currentTimeMillis() / 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
---------------------- android培訓、java培訓、期待與您交流! ----------------------