java多线程学习提升(一)

来源:互联网 发布:小幸软件工作室 编辑:程序博客网 时间:2024/05/29 07:52

准备好好的捋一捋Java中线程的使用,在最近工作的开发中,遇到好多关于多线程的问题,希望通过系统的学习能有新的认识!在以下的学习记录中,我会记录我的代码,代码中方法我会加上详细的注释,不会去介绍多线程的概念,如果有错地方,希望大牛进行指正。

(1)——首先先来回顾下线程的基本使用(两种形式,继承Thread类,实现Runnable接口):

package com.test;/** *  * @Description 传统的线程实现方式 * @author CCQ * @date 2017年7月29日 下午8:18:36 * */public class TraditionalThread {public static void main(String[] args) {/** * 第一种,继承Thread类 */Thread thread = new Thread(){@Overridepublic void run() {while(true){try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("1:" + Thread.currentThread().getName());System.out.println("2:" +this.getName());}}};thread.start();/** * 第二种,实现Runnable接口 */Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {while(true){try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("1:" + Thread.currentThread().getName());}}});thread2.start();/** * 在同时实现Runnable接口和继承Thread类的情况下 * 结果应该是执行的继承Thread类的run方法 * 参见源码: *private Runnable target; * @Override *  public void run() { *       if (target != null) { *           target.run(); *       } *   } * 可知,当继承了Thread方法,重写了run方法,则执行此时的run方法中的内容 * 否则,执行父类的run方法,判断target是不是null,不是,则调用实现Runnable中的run方法 */new Thread(new Runnable() {public void run() {while(true){try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Runnable:" + Thread.currentThread().getName());}}}){@Overridepublic void run() {while(true){try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Thread:" + Thread.currentThread().getName());}}}.start();}}

(2)——然后是定时器的使用(Timer,TimerTask)

package com.test;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.CountDownLatch;/** *  * @Description 传统的定时器实现方式 * @author CCQ * @date 2017年7月29日 下午10:35:30 * */public class TraditionalTimer {private static int COUNT = 0;public static void main(String[] args) {/** * 炸弹只爆炸一次,在2秒后爆炸 *//*new Timer().schedule(new TimerTask() {@Overridepublic void run() {System.out.println("炸弹爆炸了!!!");}}, 2000);*//** * 炸弹在5秒后爆炸,并且,以后每隔2秒爆炸一次 *//*new Timer().schedule(new TimerTask() {@Overridepublic void run() {System.out.println("炸弹爆炸了!!!");}}, 5000,2000);*//** * 炸弹过2秒爆炸,过4秒再爆炸;再过2秒再爆炸,再过4秒再爆炸、、、循环往复 *///第一种实现方案:借助一个内部类,自己调用自己实现/*class myTimerTask extends TimerTask{@Overridepublic void run() {COUNT = (COUNT + 1)%2;System.out.println("炸弹爆炸了!!!");new Timer().schedule(new myTimerTask(),2000 + COUNT * 2000);}}new Timer().schedule(new myTimerTask(), 2000);*///第二种方案:定义一个myTimerTask1,炸弹2秒爆炸,定义一个myTimerTask2,炸弹4秒爆炸,//两个炸弹任务互相调用new Timer().schedule(new myTimerTask1(), 2000);while(true){System.out.println(new Date().getSeconds());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}
package com.test;import java.util.Timer;import java.util.TimerTask;/** *  * @Description 定时炸弹任务1 * @author CCQ * @date 2017年7月29日 下午11:12:59 * */public class myTimerTask1 extends TimerTask{@Overridepublic void run() {System.out.println("炸弹爆炸了!!!");new Timer().schedule(new myTimerTask2(),2000);}}
package com.test;import java.util.Timer;import java.util.TimerTask;/** *  * @Description 定时炸弹任务2 * @author CCQ * @date 2017年7月29日 下午11:13:17 * */public class myTimerTask2 extends TimerTask{@Overridepublic void run() {System.out.println("炸弹爆炸了!!!");new Timer().schedule(new myTimerTask1(),4000);}}