java 线程的学习心得(知了堂Java线程详解)
java 线程的学习心得(知了堂Java线程详解)package com.cjg.xiancheng; public class threadDaemon { public static void main(String[] args) { me me = new me(); parents parents = new parents(); //设置守护线程 Thread thread = new Thread(me); thread.setDaemon(true); thread.start(); new Thread(parents).start(); } } class me implements Runnable{ @Override public void run() {
1、线程的状态


package com.cjg.xiancheng;
public class threadStop implements Runnable   {
    //如果不填 boolean的默认值为flase
    private  boolean flag=true;
    @Override
    public void run() {
        int i =1;
       while (flag){
           System.out.println("****" i  );
       }
    }
    public void stop(){
        this.flag=false;
    }
    public static void main(String[] args) {
        threadStop threadStop = new threadStop();
        new Thread(threadStop "me").start();
        for (int i = 0; i < 1000; i  ) {
            System.out.println("main" i);
            if (i==500){
                threadStop.stop();
                System.out.println("stop=============================================================================");
            }
        }
    }
}
    
package com.cjg.xiancheng;
import java.text.SimpleDateFormat;
import java.util.Date;
public class threadTime {
    public static void main(String[] args) throws InterruptedException {
        f();
        while (true){
            //打印当前系统的时间
            Date date = new Date(System.currentTimeMillis());
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("yyyy--MM--dd HH:mm:ss").format(date));
        }
    }
    public static void f() throws InterruptedException {
        int i=10;
        while (true){
            Thread.sleep(100);
            System.out.println(i--);
            if (i==0)break;
        }
    }
}2、线程的礼让
    

package com.cjg.xiancheng;
public class threadYield {
    public static void main(String[] args) {
        didi didi = new didi();
        new Thread(didi "一号弟弟").start(); new Thread(didi "二号弟弟").start();
    }
}
class didi implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() "start");
        Thread.yield();
        System.out.println(Thread.currentThread().getName() "end");
    }
}
    
Join
◆Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞。
package com.cjg.xiancheng;
public class threadJoin implements  Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 1000; i  ) {
            System.out.println(Thread.currentThread().getName() i);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        threadJoin threadJoin = new threadJoin();
        Thread thread = new Thread(threadJoin  "vip");
                thread.start(); //线程已经开始
        for (int i = 0; i < 500; i  ) {
            System.out.println(Thread.currentThread().getName() i);
            if (i==100){
                //知道join线程结束都只执行join线程
                thread.join();
            }
        }
    }
}3、线程的状态的查看
    

package com.cjg.xiancheng;
public class threadStatus {
    public static void main(String[] args) throws InterruptedException {
        Thread thread =new Thread(()->{
            for (int i = 0; i <5 ; i  ) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("============================================================================");
        });
        Thread.State state = thread.getState();
        System.out.println(state);
        thread.start();
        while (state !=Thread.State.TERMINATED){
            Thread.sleep(100);
          state = thread.getState();
            System.out.println(state);
        }
    }
}4、线程的优先级
    
优先级低只是意味着获得调度的概率低。并不是优先级低就不会被调用了。这都是看CPU的调度。
可能会造成性能倒置
package com.cjg.xiancheng;
public class threadMore {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() "的" Thread.currentThread().getPriority());
        more more = new more();
        Thread thread = new Thread(more);  Thread thread1 = new Thread(more);  Thread thread2 = new Thread(more);
        thread.setPriority(Thread.MAX_PRIORITY);
        thread1.setPriority(4);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread.start(); thread1.start(); thread2.start();
    }
}
class more implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() "的" Thread.currentThread().getPriority());
    }
}5、线程守护
    
◆线程分为用户线程和守护线程
◆虚拟机必须确保用户线程执行完毕
◆虚拟机不用等待守护线程执行完毕
◆如:后台记录操作日志、监控内存、垃圾回收等待……
package com.cjg.xiancheng;
public class threadDaemon {
    public static void main(String[] args) {
        me me = new me();
        parents parents = new parents();
        //设置守护线程
        Thread thread = new Thread(me);
        thread.setDaemon(true);
        thread.start();
        new Thread(parents).start();
    }
}
class me implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("我守护着你");
        }
    }
}
class parents implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <= 36500; i  ) {
            System.out.println("爸妈开心的活了" i);
        }
        System.out.println("happy end");
    }
}6、线程同步并发
    
队列加锁,保证线程安全,对应sql数据库的回滚。
◆由于同一进程的多个线程共享同一块存储空间,在带来方便的同时,也带来了访问冲突问题,为了保证数据在方法中被访问时的正确性,在访问时加入锁机制synchronized,当一个线程获得对象的排它锁,独占资源,其他线程必须等待,使用后释放锁即可,存在以下问题:
◆一个线程持有锁会导致其他所有需要此锁的线程挂起;
◆在多线程竞争下,加锁、释放锁会导致比较多的上下文切换和调度延时,引 起性能问题;
◆如果一个优先级高的线程等待一个优先级低的线程释放锁,会导致优先级倒置,引起性能问题。
==每个线程在自己的工作内存交互,内存控制不当会造成数据不一致==
package com.cjg.xiancheng;
public class syn {
    public static void main(String[] args) {
        buy buy = new buy();
        new Thread(buy "11111").start();  new Thread(buy "2222222222").start();  new Thread(buy "33333333333").start();
    }
}
class  buy implements  Runnable{
    private boolean flag=true;
    private int tiket = 10;
    @Override
    public void run() {
        while (flag){
            try {
                buyTikets();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private synchronized void buyTikets() throws InterruptedException {
        if (tiket<=0){
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName() "获得第" tiket-- "票");
        Thread.sleep(100);
    }
}
//之前加锁的是 this  如果加锁不为 this 用程序块代替  . 注意索的是谁  需要增删改的对象
synchronized (对象 obj){
}          




