下午看了张孝祥老师的多线程之后,自己也开始试着做了一个关于空中网面试题目的
看到题目之后自己先做了一个,之后再看老师的代码。。 果然是NB啊~~ 这么简单就解决了。。。
原先题目为:现在程序同时启动4个线程来调用TestDo.doSome(key,value) 方法,由于TestDo.doSome(key,value)方法代码暂停1秒,然后打印时间,所以打印出来的时间相同
例如:
begin:1324969877
1:1:1324969878
4:4:1324969878
3:3:1324969878
1:3:1324969878
1:2:1324969878
1:4:1324969879
现在使用多个线程调用TestDo.doSome(key,value) 方法, 当线程key都为1的时候,他们中的一个要比其他的慢一秒输出结构(需要互斥),
例如
begin:1324969877
1:1:1324969878
4:4:1324969878
3:3:1324969878
1:3:1324969879
1:2:1324969880
1:4:1324969881
题目源代码 如下:
package vincent.test;
import java.util.concurrent.Semaphore;
//整个类不可改动
public class KongZhong3 extends Thread {
private TestDo testDo;
private String key ;
private String value;
public KongZhong3(String key,String key2,String value){
this.testDo = TestDo.getInstance();
this.key = key+key2;
this.value = value;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
KongZhong3 a = new KongZhong3("1","","1");
KongZhong3 b = new KongZhong3("1","","2");
KongZhong3 t1 = new KongZhong3("1","","3"); // 为了测试,,我自己多加了几个,,原题目为4个
KongZhong3 t2 = new KongZhong3("1","","4");
KongZhong3 c = new KongZhong3("3","","3");
KongZhong3 d = new KongZhong3("4","","4");
System.out.println("begin:"+System.currentTimeMillis()/1000);
a.start();
b.start();
t1.start();
t2.start();
c.start();
d.start();
}
public void run(){
testDo.doSome(key, value);
}
static class TestDo{
private TestDo(){}
private static TestDo _instance = new TestDo();
public static TestDo getInstance(){
return _instance;
}
public void doSome(Object key,String value){
// { } 中代码不可改动
{
try{
Thread.sleep(1000);
System.out.println(key + ":" + value + ":" + System.currentTimeMillis()/1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
之后 上我自己先前没看老师代码只看题目写的---
import java.util.concurrent.Semaphore;
//整个类不可改动
public class KongZhong3 extends Thread {
private TestDo testDo;
private String key ;
private String value;
public KongZhong3(String key,String key2,String value){
this.testDo = TestDo.getInstance();
this.key = key+key2;
this.value = value;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
KongZhong3 a = new KongZhong3("1","","1");
KongZhong3 b = new KongZhong3("1","","2");
KongZhong3 t1 = new KongZhong3("1","","3");
KongZhong3 t2 = new KongZhong3("1","","4");
KongZhong3 c = new KongZhong3("3","","3");
KongZhong3 d = new KongZhong3("4","","4");
System.out.println("begin:"+System.currentTimeMillis()/1000);
a.start();
b.start();
t1.start();
t2.start();
c.start();
d.start();
}
public void run(){
testDo.doSome(key, value);
}
static class TestDo{
private TestDo(){}
Semaphore sp = new Semaphore(1);
private static TestDo _instance = new TestDo();
static int index = 0;
public static TestDo getInstance(){
return _instance;
}
public void doSome(Object key,String value){
if(key.equals("1") )
{
try {
sp.acquire();
if(index != 0) //看看是否是第一个
Thread.sleep(1000);
index ++;
sp.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// { } 中代码不可改动。。。 看到这个题目我以为不能碰。。汗 ~~~ 所以前面用了信号量作为判断
{
try{
Thread.sleep(1000);
System.out.println(key + ":" + value + ":" + System.currentTimeMillis()/1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
static class TestDo{
private TestDo(){}
private static TestDo _instance = new TestDo();
public static TestDo getInstance(){
return _instance;
}
private CopyOnWriteArrayList<Object> keys = new CopyOnWriteArrayList<Object>();
public void doSome(Object key,String value){
Object o = key;
if(keys.contains(o)){
for(Iterator<Object> ite = keys.iterator(); ite.hasNext();){
Object oo = ite.next();
if(oo.equals(o))
o = oo;
}
}else
{
keys.add(o);
}
// { } 中代码不可改动
synchronized (o)
{
try{
Thread.sleep(1000);
System.out.println(key + ":" + value + ":" + System.currentTimeMillis()/1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
使用列表存储对象,之后需要对比的时候直接从列表里面取出对象再作为对比的KEY,这个就是这个题目的解答了。。
当然这个题目解决方法很多,使用lock也是可以的。。 不过为了通用 最终还是需要用类似 张孝祥老师的这种,先用列表存储数据再对比。