java多线程高级应用_Java多线程与并发库高级应用-传统线程机制回顾

1.传统线程机制的回顾

1.1创建线程的两种传统方式

在Thread子类覆盖的run方法中编写运行代码

//1.使用子类,把代码放到子类的run()中运行

Thread thread = newThread() {

@Overridepublic voidrun() {while (true) {try{

Thread.sleep(500);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName());

}

}

};

thread.start();

>涉及一个以往的知识点:能否在run方法声明上抛出异常,以便省略run方法内部对Thread.sleep()语句的try...catch处理InterrunptedException?

在覆写祖先类方法或实现接口方法(覆写run方法,而本源run方法没有做抛出声明),所以你无法做抛出声明

在传递给Thread对象的Runnable对象的run方法中编写代码

publicThread(Runnable target) {

init(null, target, "Thread-" + nextThreadNum(), 0);

}

在init方法中对Thread对象的target进行复制

Thread的run方法

/*** If this thread was constructed using a separate

* Runnable run object, then that

* Runnable object's run method is called;

* otherwise, this method does nothing and returns.

*

* Subclasses of Thread should override this method.

*

*@see#start()

*@see#stop()

*@see#Thread(ThreadGroup, Runnable, String)*/@Overridepublic voidrun() {if (target != null) {

target.run();

}

}

//2.代码放在Runnable对象中,Runnable是代码运行的宿主

Thread thread2 = new Thread(newRunnable() {

@Overridepublic voidrun() {while (true) {try{

Thread.sleep(500);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName());

}

}

});

thread2.start();

>问题:如果在Thread子类覆盖的run方法总编写了运行代码,也为Thread子类对象传递了一个Runnable对象,那么,线程执行的是哪个run方法?

子类的run()覆盖了父类的run(),如果没有覆盖父类的run(),会找Runnable

//3.使用的是子类中的run方法

new Thread(newRunnable() {

@Overridepublic voidrun() {while(true){try{

Thread.sleep(500);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("runnable :"+Thread.currentThread().getName());

}

}

}){public voidrun() {while(true){try{

Thread.sleep(500);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("thread :"+Thread.currentThread().getName());

}

};

}.start();

>涉及到一个以往的知识点:匿名内部类对象的构造方法如何调用父类的非默认构造方法?

>多线程机制会提高程序的运行效率么?

单个cpu下不会提高运行效率,反而性能更低

Java中sleep和wait的区别

① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类。

sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线程里调用b的sleep方法,实际上还是a去睡觉,要让b线程睡觉要在b的代码中调用sleep。

② 锁: 最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。

sleep不出让系统资源;wait是进入线程等待池等待,出让系统资源,其他线程可以占用CPU。一般wait不会加时间限制,因为如果wait线程的运行资源不够,再出来也没用,要等待其他线程调用notify/notifyAll唤醒等待池中的所有线程,才会进入就绪队列等待OS分配系统资源。sleep(milliseconds)可以用时间指定使它自动唤醒过来,如果时间不到只能调用interrupt()强行打断。

Thread.sleep(0)的作用是“触发操作系统立刻重新进行一次CPU竞争”。

③ 使用范围:wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用。

synchronized(x){

x.notify()

//或者wait()

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值