前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多线程之传统多线程

多线程之传统多线程

作者头像
xiangzhihong
发布2018-02-01 15:52:36
8730
发布2018-02-01 15:52:36
举报
文章被收录于专栏:向治洪向治洪

Contents

传统线程技术

传统创建线程方式

1.继承Thread类,覆盖run方法

代码语言:javascript
复制
Thread t = new Thread();
t.start();

2.实现Runnable接口

Runnable不是线程,是线程要运行的代码的宿主。

1.看看Thread类源码,捋清Runnable,target,run,start关系

  • Runnable是一个接口
  • targetThread类中类型为Runnable,名为target的属性
  • runThread类实现了Runnable的接口,重写的方法。
  • start是启动线程的方法
  • Thread类中,调用关系为:start->start0->run->target.run

Thread类的run方法源码

代码语言:javascript
复制
public void run() {
    if (target != null) {
        target.run();
    }
}

Thread类的target属性

代码语言:javascript
复制
/* What will be run. */
private Runnable target;

target属性由private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc)方法初始化。init方法在Thread类的构造方法里被调用

2.匿名内部类对象的构造方法如何调用父类的非默认构造方法

传统定时器技术

API:

java.util:Class Timer

例子:一个定时器实现交替2秒、3秒打印

代码语言:javascript
复制
static int count = 0;
public static void main(String[] args) {

    class MyTimerTask extends TimerTask{

        @Override
        public void run() {
            System.out.println(Thread.currentThread()+" bomb!");
            new Timer().schedule(new MyTimerTask(), 2000+1000*(count++%2));
        }
    }
    //3s后开启定时器
    new Timer().schedule(new MyTimerTask(),3000);
}

可以使用quarlz开源工具

互斥

关键字:synchronized,检查锁对象

  • synchronized(this)
  • synchronized void function(){}
  • synchronized(A.class)

同步

经验:

要用到共同数据(包括同步锁)或共同算法的若干个方法应该归在同一个类身上,这种设计体现了高聚类和程序的健壮性。

同步互斥不是在线程上实现,而是在线程访问的资源上实现,线程调用资源。

例子: 子线程循环5次,主线程循环10次,如此交替50次

设计:

使用一个Business类来包含子线程和主线程要运行的代码,从而,该类的对象成为加锁的对象。同步互斥在该类实现,由线程调用该类的方法,即调用了资源。

代码:

代码语言:javascript
复制
/**
 * Created by brian on 2016/2/4.
 */
public class TraditionalThreadCommunication {
    public static void main(String[] args) {
        Business business = new Business();
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        for(int i=1;i<=50;i++){
                            business.sub(i);
                        }
                    }
                }
        ).start();

        for(int i=1;i<=50;i++){
            business.main(i);
        }

    }
}

class Business{
    private boolean bShouldSub = true;

    public synchronized void sub(int i){
        while(!bShouldSub){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for(int j=1;j<=5;j++){
            System.out.println("sub thread count "+j+","+i+"/50");
        }
        bShouldSub = false;
        this.notify();
    }
    public synchronized void main(int i){
        while(bShouldSub){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for(int j=1;j<=10;j++){
            System.out.println("main thread count "+j+","+i+"/50");
        }
        bShouldSub = true;
        this.notify();
    }
}

判断条件时,while与if的区别:while防止伪唤醒

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 传统线程技术
    • 传统创建线程方式
      • 传统定时器技术
        • 互斥
          • 同步
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档