关于JAVA线程面试题

java

关于JAVA线程面试题

1.doOther需要等到doSome结束吗

 1 package javase.exam1;

2 /**

3 * 面试题:doOther需要等到doSome结束吗

4 * 不需要

5 * synchronized 作用在实例方法上 相当于this 而doother没有synchronized

6 *

7 * @author yumu

8 *

9 */

10 public class Exam01 {

11

12 public static void main(String[] args) throws InterruptedException {

13 MyClass mc=new MyClass();

14

15 Thread t1=new MyThread(mc);

16 Thread t2=new MyThread(mc);

17 t1.setName("t1");

18 t2.setName("t2");

19

20 t1.start();

21 Thread.sleep(1000);

22 t2.start();

23 }

24

25 }

26 class MyThread extends Thread{

27 private MyClass mc;

28 public MyThread(MyClass mc){

29 this.mc=mc;

30 }

31 public void run(){

32 if(Thread.currentThread().getName().equals("t1")){

33 mc.doSome();

34

35 }

36 if(Thread.currentThread().getName().equals("t2")){

37 mc.doOther();

38

39 }

40

41 }

42 }

43 class MyClass{

44 // public synchronized static void doSome(){ synchronized出现在静态方法上,是类锁,只有一把,需要等待

45 public synchronized void doSome(){

46 System.out.println("doSome begin");

47 try {

48 Thread.sleep(1000*10);

49 } catch (InterruptedException e) {

50

51 e.printStackTrace();

52 }

53 System.out.println("doSome over");

54 }

55 public void doOther(){

56 System.out.println("doother begin");

57

58 System.out.println("doother over");

59 }

60 }

 

2.doOther需要等到doSome结束吗

 1 package javase.exam1;

2 /**

3 * 面试题:doOther需要等到doSome结束吗

4 * 不需要

5 * myclass对象两个 两把锁

6 *

7 * @author yumu

8 *

9 */

10 public class Exam02 {

11

12 public static void main(String[] args) throws InterruptedException {

13 MyClass mc1=new MyClass();

14 MyClass mc2=new MyClass();

15

16 Thread t1=new MyThread1(mc1);

17 Thread t2=new MyThread1(mc2);

18 t1.setName("t1");

19 t2.setName("t2");

20

21 t1.start();

22 Thread.sleep(1000);

23 t2.start();

24 }

25

26 }

27 class MyThread1 extends Thread{

28 private MyClass mc;

29 public MyThread1(MyClass mc){

30 this.mc=mc;

31 }

32 public void run(){

33 if(Thread.currentThread().getName().equals("t1")){

34 mc.doSome();

35

36 }

37 if(Thread.currentThread().getName().equals("t2")){

38 mc.doOther();

39

40 }

41

42 }

43 }

44 class MyClass1{

45 public synchronized void doSome(){

46 System.out.println("doSome begin");

47 try {

48 Thread.sleep(1000*10);

49 } catch (InterruptedException e) {

50

51 e.printStackTrace();

52 }

53 System.out.println("doSome over");

54 }

55 public synchronized void doOther(){

56 System.out.println("doother begin");

57

58 System.out.println("doother over");

59 }

60 }

 

3.关于Thread.sleep()方法 线程t会进入休眠吗

 

 1 package javase.thread;

2 /**

3 * 关于Thread.sleep()方法面试题

4 * 线程t会进入休眠吗

5 * @author yumu

6 *

7 */

8 public class ThreadTest06 {

9

10 public static void main(String[] args) throws InterruptedException {

11 MyThread3 t=new MyThread3();

12 t.setName("t2");

13 t.start();

14 //调用sleep 静态方法

15 t.sleep(2000); //在执行的时候还是会转换成Thread.sleep(2000);

16 //这换代码是让当前线程进入休眠,他出现在main方法

17 System.out.println("你好");//2秒后输出

18 }

19

20 }

21 class MyThread3 extends Thread{

22 public void run(){

23 for(int i=0;i<10;i++){

24 System.out.println(Thread.currentThread().getName()+"--->"+i);

25 }

26 }

27 }

 

线程知识点:

 练习题:

以上是 关于JAVA线程面试题 的全部内容, 来源链接: utcz.com/z/390038.html

回到顶部