java关于多线程的面试题(三道)
2017-07-06 14:09
453 查看
第一题题目
原代码
第二题题目
第二题题目原题目的运行效果
想要实现的效果
第三题题目
第一题的原代码
题目是空中网以往的面试题 来自已故技术前辈张孝祥老师的相关学习资料
下一篇会给出参考答案
原代码
第二题题目
第二题题目原题目的运行效果
想要实现的效果
第三题题目
第一题的原代码
public class Test { public static void main(String[] args){ System.out.println("begin:"+(System.currentTimeMillis()/1000)); for(int i=0;i<16;i++){ //这行代码不能改动 final String log = ""+(i+1);//这行代码不能改动 { 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(); } } }第二题题目原代码
public class Test { public static void main(String[] args) { System.out.println("begin:"+(System.currentTimeMillis()/1000)); for(int i=0;i<10;i++){ //这行不能改动 String input = i+""; //这行不能改动 String output=TestDo.doSome(input); System.out.println(Thread.currentThread.getName()+":"+output); } } } //不能改动此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; } }第三题题目原代码
//不能改动此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"+""; 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; } public void doSome(Object key, String value) { // 以大括号内的是需要局部同步的代码,不能改动! { try { Thread.sleep(1000); System.out.println(key+":"+value + ":" + (System.currentTimeMillis() / 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
题目是空中网以往的面试题 来自已故技术前辈张孝祥老师的相关学习资料
下一篇会给出参考答案
相关文章推荐
- Java中关于多线程的面试题
- Java关于多线程、并发、工具类的18道面试官必问经典面试题
- 关于google的JAVA面试题我的解法
- java高级多线程编程--关于线程的停止问题
- 一道关于finally的看似非常简单但很多人都做错的Java面试题
- 关于微软面试题:"四人过桥"问题的思考——“n人过桥”问题的演进(Java实现)
- 关于微软面试题:"四人过桥"问题的思考——“n人过桥”问题的演进(Java实现)
- 一个blog,关于java方面的面试题
- JAVA面试题解惑系列(十)——话说多线程
- Java语言深入:关于多线程程序模型研究
- java高级多线程编程--关于线程的停止问题
- JAVA面试题解惑系列(十)——话说多线程
- java高级多线程编程--关于线程的停止问题
- JAVA面试题解惑系列(十)——话说多线程
- JAVA面试题解惑系列(十)――话说多线程
- 关于java方法多线程访问自己的理解
- 关于这java类实现多线程的困惑
- java高级多线程编程--关于线程的停止问题
- JAVA面试题解惑系列(十)——话说多线程
- Java多线程编程——关于notify(), wait()的问题