线程第二章

实际上,main方法是由一个线程运行的。

获取执行当前语句的线程 Thread.currentThread( )

/**
 * 获取线程
 * @author Administrator
 *
 */
public class getThreadName {

    public static void main(String[] args) {
        Thread t = Thread.currentThread();
        System.out.println("调用main方法的线程是:" + t);
        testCurrent();

        Thread t1 = new Thread(){
            public void run(){
                Thread myt = Thread.currentThread();
                System.out.println("自己创建的线程是:" + myt);
                testCurrent();
            }
        };
        t1.start();
    }

    public static void testCurrent(){
        Thread t = Thread.currentThread();
        System.out.println("调用testCurrent方法的线程是:" + t);
    }

}

获取线程信息

**关于ID**:不能为null,唯一性

long getId( ) 线程标识符
String getName( ) 返回线程的名称
…….

/**
 * 获取线程信息
 * @author Administrator
 *
 */
public class GetThreadInfo {
    public static void main(String[] args) {
        Thread t = Thread.currentThread();
        //获取id
        long id = t.getId();
        System.out.println("id:" + id);

        //获取线程名称,格式Thread-X,调用main的线程除外
        String name = t.getName();
        System.out.println("name:" + name);

        //获取线程优先级
        int p = t.getPriority();
        System.out.println("优先级:" + p);

        //获取线程状态
        System.out.println("线程状态(state):" + t.getState());

        //判断线程是否还活着
        System.out.println("线程活着吗:" + t.isAlive());

        //获取线程是否为后台线程
        System.out.println("是后台线程吗:" + t.isDaemon());

        //判断线程是否被中断
        System.out.println("线程被中断了吗:" + t.isInterrupted());
    }

}

线程优先级
唯一干涉线程调度的办法:更改线程优先级,改善线程获取时间片的几率。

优先级分为10级,1-10

/**
 * 线程优先级
 * 1-10
 * @author Administrator
 *
 */
public class ThreadPriority {
    public static void main(String[] args){
        Thread max = new Thread(){
            public void run(){
                for(int i = 0 ;i < 5000;i++){
                    System.out.println("max");
                }
            }
        };

        Thread norm = new Thread(){
            public void run(){
                for(int i = 0 ;i < 5000;i++){
                    System.out.println("norm");
                }
            }
        };

        Thread min = new Thread(){
            public void run(){
                for(int i = 0 ;i < 5000;i++){
                    System.out.println("min");
                }
            }
        };

        max.setPriority(Thread.MAX_PRIORITY);
        min.setPriority(Thread.MIN_PRIORITY);

        max.start();
        norm.start();
        min.start();
    }
}

守护线程
又名后台线程,和普通线程在表现上没什么区别,所有前台线程结束时,后台线程随之结束。
进程结束的条件:所有前台线程都结束了。

/**
 * 后台线程
 * 所有前台线程都结束时,后台线程结束,无论它是否还在运行。
 * @author Administrator
 *
 */
public class BackgroundThread {
    public static void main(String[] args) {
        Thread rose = new Thread(){
            public void run(){
                for(int i = 0;i < 10;i++){
                    System.out.println("rose:Let me go!");
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException e){

                    }
                }
                System.out.println("rose:AAAAAaaaa.....");
                System.out.println("音效:噗通..");
            }
        };

        Thread jack = new Thread(){
            public void run(){
                while(true){
                    System.out.println("jack:you jump,I jump!");
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException e){

                    }
                }
            }
        };

        rose.start();
        jack.setDaemon(true);
        jack.start();
        System.out.println("main方法结束了");
    }

}

睡眠阻塞
线程进入sleep阻塞状态时,会停顿,此时的状态介于Runnable和Running之间,直到sleep时间结束时,线程会回到Runnable状态。

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 通过睡眠阻塞实现电子表
 * @author Administrator
 *
 */
public class Watch {

    public static void main(String[] args) {
        SimpleDateFormat sdf =  new SimpleDateFormat("HH:mm:ss");
        while(true){
            Date now = new Date();
            System.out.println(sdf.format(now));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }

    }

}

join 等你走了我再出去玩
两个线程A和B,需求:线程A结束后执行线程B。注意:用final修饰要调用的
线程A。

/**
 * join方法
 * @author Administrator
 *
 */
public class ThreadJoin {

    public static void main(String[] args) {
        //下载图片的线程
        //局部内部类,区别于匿名内部类,定义在方法中
        final Thread download = new Thread(){
            public void run(){
                for(int i = 0;i <=100;i++){
                    System.out.println("已完成"+i+"%");
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        //显示图片的线程
        //main方法中定义了一个局部内部类show,在局部内部类中如果想引用该
        //方法的局部变量,被引用的局部变量必须用final修饰。
        final Thread show = new Thread(){
            public void run(){
                try {
                    download.join();
                    System.out.println("显示图片");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        download.start();
        view.start();

    }

}

局部内部类
区别于匿名内部类,定义在方法中

并发安全问题

生活情境:渣男A,在某行有个账户,名下有一张借记卡和一张存折。渣男想:我拿存折去银行取钱的同时叫渣男C去拿着卡去取款机上取钱,这样我就可能取到两倍的钱。然而我早就看穿了一切…………….
猫跑电信杆,想救它,把电闸拉下,再去救猫,考虑到可能电闸可能被合上,把电闸箱锁了。

多个线程并发读写同一个临界资源时会发生线程并发安全问题
产生多线程并发安全的原因是:多线程并发操作同一数据
解决多线程并发安全问题办法是:将异步的操作变成同步的操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值