JAVA回顾——线程1

线程的实现

因为是回顾所以太基础的东西也不会这么写


一般常用的情况下 线程有两种 实现方式
1.继承Thread类
2.实现Runable接口

注意事项

1.采用多线程时候 代码的执行顺序和调用顺序是无关的
2.开启线程时候如果直接调用run 方法,那么程序将一直在main 也就是主线程中运行
3.开启线程就是异步的效果,如果在主线程中调用Run 方法开启线程那么这样是同步而不是异步
4.线程的开启具有随机性 由Cpu 随机开启
5.线程.start方法的顺序不代表线程启动顺序
6.当线程使用锁 的时候要注意for循环 否则很有可能在用一个循环跑完整个for循环

方法

currentThread()
可以返回当前正在调用那个线程的信息
isAlive()
判断当前线程是否处于活动状态
什么是活动状态呢? 活动状态就是线程已经启动但是尚未终止。线程处于正在运行的状态。就任务线程存活

继承Thread 接口(主意事项1解释)

public class one extends Thread {

    public void run()
    {

        System.out.println(Thread.currentThread().getName()
                +"MY Thread");
    }

}


public class oneRun {
    public static void main(String[] args) {
        one mythread = new one();
        mythread.start();
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

}

//输出结果为
//main运行结束
//Thread-0MY Thread

// 如果将start 改为Run
 public class oneRun {
    public static void main(String[] args) {
        one mythread = new one();
        mythread.run();
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

}

//输出结果为
//mainMY Thread
//main运行结束


由上面例子中main运行结束 在 Thread-0MY Thread 可以充分说明 采用多线程时候 代码的执行顺序和调用顺序是无关的

runable接口实现

public class one implements Runnable {
    public void run() {

        System.out.println(Thread.currentThread().getName()
                +"线程开启了");
    }

    }

public class oneRun {
    public static void main(String[] args) {
        one mythread = new one();
        Thread thread = new Thread(mythread);
        thread.start();
        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

}

//输出结果为

//main运行结束
//Thread-0线程开启了



线程.start方法的顺序不代表线程启动顺序

public class one  extends Thread {
    private int i ;
    public one(int i)
    {
        super();
        this.i = i;
    }

    public void run() {

        System.out.println(i);

    }
}
 public class oneRun {
    public static void main(String[] args) {
        one mythread1 = new one(1);
    /*  Thread thread = new Thread(mythread);
        thread.start();*/
        one mythread2 = new one(2);
        one mythread3 = new one(3);
        one mythread4 = new one(4);
        one mythread5 = new one(5);
        one mythread6 = new one(6);
        mythread1.start();
        mythread2.start();
        mythread3.start();
        mythread4.start();
        mythread5.start();
        mythread6.start();

        System.out.println(Thread.currentThread().getName()+"运行结束");
    }

}

输入结果
2
5
4
main运行结束
3
6
1

#多个线程同时访问一个变量
public class one extends Thread {
private int count =5 ;
public void run()
{
count–;
System.out.println(Thread.currentThread().getName()+

         "  "  + count  );

}
}
public class oneRun {
public static void main(String[] args) {
one mythread1 = new one();
/* Thread thread = new Thread(mythread);
thread.start();
one mythread2 = new one(2);
one mythread3 = new one(3);
one mythread4 = new one(4);
one mythread5 = new one(5);
one mythread6 = new one(6);
mythread1.start();
mythread2.start();
mythread3.start();
mythread4.start();
mythread5.start();
mythread6.start();*/
Thread one = new Thread(mythread1);
Thread two = new Thread(mythread1);
Thread three = new Thread(mythread1);
Thread four = new Thread(mythread1);
Thread five = new Thread(mythread1);
one.start();
two.start();
three.start();
four.start();
five.start();

}

输出结果
Thread-1 1
Thread-5 1
Thread-4 0
Thread-3 1
Thread-2 1

}
上面的问题就是线程出现了安全性的问题 要解决这个问题我们就要用到线程的同步 也就是锁
今天说一个最常用的锁 synchronized

我们同步后的代码块


public class one  extends Thread {
    private int count =5 ;
 synchronized public void run()
 {
     count--;
     System.out.println(Thread.currentThread().getName()+

             "  "  + count  );

 }
}
输出结果
Thread-1  4
Thread-4  3
Thread-5  2
Thread-3  1
Thread-2  0


方法

currentThread()
可以返回当前正在调用那个线程的信息

isAlive()

public class two extends Thread {
    public void run()
    {
        System.out.println("线程已经启动");
    }

}
public class twoRun {
public static void main(String[] args) {
    two t = new two();
    Thread thread = new Thread(t);
    System.out.println(".start之前" + thread.isAlive());
thread.start();
/*  try {
        thread.sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/
    System.out.println(".start之后" + thread.isAlive());


}
}
输出结果
.start之前false
.start之后true
线程已经启动

 为啥在mian函数中他还存活呢  因为程序运行太快该线程还在   可以试试 将try crach 的注释去掉  当线程等待500毫秒之后   会有不同结果


你可能感兴趣的:(JAVA回顾——线程1)