java 线程Thread 技术--线程状态与同步问题

线程技术第三篇:

线程的状态:  

  1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源

  2.可运行状态:当线程调用start 方法将为线程分配必须的系统资源,安排其运行,并调用线程体中的run方法,这样线程就处于可运行状态

  3.不可运行状态:当调用了sleep()方法,或者object 类中的wait() 方法,线程输入输出阻塞

  4.消亡状态:run 方法执行完后,就会自动消亡

线程优先级:

  1.线程创建时,子继承父的线程优先级

  2.可以通过线程方法 setPriority()设置优先级

  3.优先级在1-10整数之间

多线程的同步:

  情景:在所线程环境里,可能有多个线程试图同时访问一个有限的资源,所以我们必须对这种潜在的资源冲突进行预防,

  解决方法就是:在线程使用一个资源时候,对其加锁,访问资源的第一个线程为其加上锁之后,其他线程不可以进行对其的访问,除非锁被解除

  synchronized 关键字:(都是重点)

  1.当synchronized 关键字修饰一个方法的时候,那么该方法就是同步方法;

  2. java 每个对象都有一把锁(监视器),当访问某个对象的synchronized 的方法时,表示将该对象上锁,那么其他的线程就无法访问synchronized 方法了,直到之前那个线程执行方法完毕后或者抛出异常,那么该对象的锁就会释放掉;其他线程才能访问synchronized 方法  

  3.如果一个对象有多个synchronized 方法,在某一时刻某一线程已经进入某个synchronized 方法,那么在该方法没有执行完毕之前,其他线程都无法访问

  这个对象的任何synchronized 方法

  4.在遇到synchronized static 修饰的方法时候,它的锁不是synchronized 方法所在的对象,而是是当前对象所对应的的class 对象;因为static 是基于类的;

Example:

  执行以下代码,我们可以明显看出,执行结果是有顺序的,因为synchronized 是在对象上加锁,只有执行完一个synchronized 方法,才能执行下一个synchronized 

/**
 * this demo diaplay keywords synchronized is add lock in Object 
 * when Object has many synchronized method , and some Thread invoke some synchronized method,
 * then other Thread refuse to call on any synchronized method ,only if some synchronized method
 * invoke finish.
 * 
 * @author iscys
 *
 */
public class SynchronizedObjectLock {

    public static void main(String[] args) {
        //设置唯一对象
        Example example =new Example();
        ImplRunnable run1=new ImplRunnable(example);
        ImplRunnable2 run2=new ImplRunnable2(example);
        Thread th1 =new Thread(run1);
        Thread th2 =new Thread(run2);
        th1.start();
        th2.start();
    }

}

class Example{
    
    public synchronized  void ex() {
        for(int i=0;i<20;i++) {
            System.out.println("ex method"+i);
        }
    }
    
    public synchronized void ex2() {
        for(int i=0;i<20;i++) {
            System.out.println("ex2 method"+i);
        }
    }
}

//one instance of thread
class ImplRunnable implements Runnable{
    
    private Example example;
    //construct
    public ImplRunnable(Example example){
        this.example =example;
    }
    @Override
    public void run() {
        example.ex();
        
    }
    
}
//one instance of thread
class ImplRunnable2 implements Runnable{
    
    private Example example;
    //construct
    public ImplRunnable2(Example example){
        this.example =example;
    }
    @Override
    public void run() {
        example.ex2();
        
    }
    
}

在这个例子的基础上,我们将一个方法改为静态方法去运行:其实执行的结果是乱序的,因为被static 修饰的方法,属于类本身,它的锁对应的是class 对象

而未被static 修饰的,锁则是synchrionized 所在的对象;

  public synchronized static void ex2() {
        for(int i=0;i<20;i++) {
            System.out.println("ex2 method"+i);
        }

5.同步的第二种方式,使用同步代码块 ,线程执行会对Object 对象上锁,其他规则与synchronized 一致

private Object object =new Object();
synchronized
(object){
  //body.....   }
原创打造,多多指教
原文地址:https://www.cnblogs.com/iscys/p/9704447.html