多线程总结

创建线程方法

//1
public static void main(String[] args) {
Thread thread=new mythread();
thread.start();
    }
    static class mythread extends Thread{
        public void run(){
            Thread t1=Thread.currentThread();
            System.out.println(t1.getName());
        }
    }

//2
public static void main(String[] args) {
        mythread t1=new mythread();
Thread thread=new Thread(t1);
thread.start();
    }
    static class mythread implements Runnable{
        public void run(){
            Thread t1=Thread.currentThread();
            System.out.println(t1.getName());
        }
    }
//3
 public static void main(String[] args) {
Thread thread=new Thread(new Runnable() {
    @Override
    public void run() {
        Thread t1=Thread.currentThread();
        System.out.println(t1.getName());
    }
});
thread.start();
    }
//4
public static void main(String[] args) {
Thread thread=new Thread(()-> {
        Thread t1=Thread.currentThread();
        System.out.println(t1.getName());
});
thread.start();
    }
//5
public static void main(String[] args) {
Thread thread=new Thread(){
    public void run(){
        Thread t1=Thread.currentThread();
        System.out.println(t1.getName());
    }
};
thread.start();
//6
public static void main(String[] args) {
        FutureTask<Integer> task=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Thread t1=Thread.currentThread();
                System.out.println(t1.getName());
                return null;
            }
        });
        Thread thread=new Thread(task);
thread.start();
    }
//7
public static void main(String[] args) throws ExecutionException, InterruptedException {
        mycallable t1=new mycallable();
        FutureTask<Integer> task=new FutureTask<>(t1);
        Thread thread=new Thread(task);
        thread.start();
        System.out.println(task.get());
    }
    static class mycallable implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            Thread t1=Thread.currentThread();
            System.out.println(t1.getName());
            return 1;
        }
    }

Thread() 创建线程对象

Thread(String name) 创建线程对象,并命名

 Thread(Runnable target) 用Runnable创建线程对象

Thread(Runnable target,String name) 用Runnable创建线程对象,并命名

Thread(ThreadGroup group,Runnable target)线程分组

getId() 获取线程ID 

getName() 获取线程名称

getState() 获取线程状态   线程状态共有6种1.NEW(新建状态)2.RUNNABLE(运行状态)3.BLOCKED(阻塞状态)4.WAITING(等待状态,无限期等待)5.TIMED_WAITING(有明确结束时间的等待)6.TERMINATED(销毁状态)

getPriority() 获取线程优先级  优先级最大为10,最小为1,默认优先级为5,优先级越高获取到CPU时间片的概率越高,因此一般会先执行完。

isDaemon() 是否守护线程  守护线程在用户线程执行完的时候也会结束,无论它是否执行完毕。守护线程中创建的线程默认是守护线程,守护线程不能在start()之后设置。

setDaemon(true) 设置为守护线程

isAlive() 是否存活

interrupted()     (会清除标志位,是静态方法)

isInterrupted() 是否被中断(不会清除标志位)

interrupt() 中断线程

join() 等待线程结束再继续执行

join(long millis) 等待线程结束,最多等millis毫秒

join(long millis,int nanos)同理,但可以更高精度

public static Thread currentThread(); 返回当前线程对象的引用

yield() 让出CPU执行权

sleep(long millis) 休眠线程

TimeUnit.DAYS.sleep(1);//天

TimeUnit.HOURS.sleep(1);//小时

TimeUnit.MINUTES.sleep(1);//分

TimeUnit.SECONDS.sleep(1);//秒

TimeUnit.MILLISECONDS.sleep(1000);//毫秒

TimeUnit.MICROSECONDS.sleep(1000);//微妙

TimeUnit.NANOSECONDS.sleep(1000);//纳秒 

线程不安全元素:①抢占式执行

②多个线程修改同一个变量

③非原子性操作

④内存可⻅性

⑤指令重排序

volatile 可以解决内存可⻅性和指令重排序的问题
代码在写⼊ volatile 修饰的变量的时候:
1. 改变线程⼯作内存中volatile变量副本的值
2.  将改变后的副本的值从⼯作内存刷新到主内存
代码在读取 volatile 修饰的变量的时候:
1.  从主内存中读取volatile变量的最新值到线程的⼯作内存中
2.  从⼯作内存中读取volatile变量的副本
volatile无法解决原子性问题。
 synchronized 基本使⽤
synchronized 的基本⽤法有以下 3 种:
1. 修饰静态⽅法
2. 修饰普通⽅法
3. 修饰代码块 
 synchronized 特性
1.互斥
synchronized 会起到互斥效果, 某个线程执⾏到某个对象的 synchronized 中时, 其他线程如果也
执⾏到同⼀个对象 synchronized 就会阻塞等待.
  进⼊ synchronized 修饰的代码块, 相当于 加锁
  退出 synchronized 修饰的代码块, 相当于 解锁
2.  刷新内存
synchronized 的⼯作过程:
1. 获得互斥锁
2. 从主内存拷⻉变量的最新副本到⼯作的内存
3. 执⾏代码
4. 将更改后的共享变量的值刷新到主内存
5. 释放互斥锁
所以 synchronized 也能保证内存可⻅性.
3 ).可重⼊
synchronized 同步块对同⼀条线程来说是可重⼊的,不会出现⾃⼰把⾃⼰锁死的问题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值