java 多线程学习笔记1-概念,实现方式

来源:互联网 发布:淘宝买家诈骗怎么办 编辑:程序博客网 时间:2024/06/03 13:59
1、多线程的概念:
    线程是程序执行的一条路径, 一个进程中可以包含多条线程
    多线程并发执行可以提高程序的效率, 可以同时完成多项工作
    
    并行:就是两个任务同时运行,就是甲任务进行的同时,乙任务也在进行。(需要多核CPU)。
    并发:是指两个任务都请求运行,而处理器只能按受一个任务,就把这两个任务安排轮流进行,由于时间间隔较短,使人感觉两个任务都在运行。
    JVM启动至少启动了垃圾回收线程和主线程,所以是多线程的
    
2、多线程的实现方式:
    a、继承Thread类
        public class Demo2_Thread {
public static void main(String[] args) {
MyThread mt = new MyThread();//创建自定义类的对象
mt.start(); //开启线程

for(int i = 0; i < 3000; i++) {
System.out.println("aaa");
}
}
}
class MyThread extends Thread {//定义类继承Thread
public void run() {//重写run方法
                    System.out.println("test");                             //将要执行的代码,写在run方法中
}
}
            
      
    b、实现Runnable接口:
        public class Demo3_Runnable {
            public static void main(String[] args) {
                MyRunnable mr = new MyRunnable(); //4,创建自定义类对象
                Thread t = new Thread(mr); //5,将其当作参数传递给Thread的构造函数
                t.start(); //6,开启线程
                
                for(int i = 0; i < 3000; i++) {
                    System.out.println("bb");
                }
            }
        }
        
        class MyRunnable implements Runnable { //1,自定义类实现Runnable接口
            @Override
            public void run() { //2,重写run方法
                for(int i = 0; i < 3000; i++) { //3,将要执行的代码,写在run方法中
                    System.out.println("aa");
                }
            }
            
        }
        
        注意:实现Runnable的原理,看Thread类的构造函数,传递了Runnable接口的引用。
        
        两种方式的区别:
        继承Thread
            好处是:可以直接使用Thread类中的方法,代码简单
            弊端是:如果已经有了父类,就不能用这种方法
        实现Runnable接口
            好处是:即使自己定义的线程类有了父类也没关系,因为有了父类也可以实现接口,而且接口是可以多实现的
            弊端是:不能直接使用Thread中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂
            
        没有特殊要求使用Thread满足要求就尽量使用Thread吧。
    
3、通过匿名内部类实现多线程的方法:
    a、继承Thread:
        new Thread() { //1,new 类(){}继承这个类
public void run() {//2,重写run方法
for(int i = 0; i < 3000; i++) {//3,将要执行的代码,写在run方法中
System.out.println("aaa");
}
}
}.start();//启动线程
    b、实现Runnable接口:
        new Thread(new Runnable(){ //1,new 接口(){}实现这个接口
public void run() {//2,重写run方法
for(int i = 0; i < 3000; i++) {//3,将要执行的代码,写在run方法中
System.out.println("bb");
}
}
}).start(); 
    
    
4、获取和设置线程的名称:
    获取:通过getName()方法获取线程对象的名字
    设置线程名称:
    a、通过构造函数可以传入String类型的名字
        new Thread("111") {//通过构造函数传入
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(this.getName() + "aa");
                }
            }
        }.start();
    b、通过setName(String)方法:
        Thread t1 = new Thread() {
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(this.getName() + "aa");
                }
            }
        };
        t1.setName("111");//设置线程名称
        t1.start();
    
5、获取当前线程的对象:
    Thread.currentThread()
    
6、多线程-休眠线程:
    Thread.sleep(1000)//1000毫秒
    可以让线程等待1s后继续执行代码
    
7、多线程-守护线程:
    setDaemon(), 设置一个线程为守护线程, 该线程不会单独执行, 当其他非守护线程都执行结束后, 自动退出
        Thread t1 = new Thread() {
            public void run() {
                for(int i = 0; i < 50; i++) {
                    System.out.println(getName() + "aa");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        
        Thread t2 = new Thread() {
            public void run() {
                for(int i = 0; i < 5; i++) {
                    System.out.println(getName() + "bb");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        
        t1.setDaemon(true); //将t1设置为守护线程,t2线程结束了,t1自动结束
        t1.start();
        t2.start();
8、多线程-加入线程:
    join(), 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续。
        Thread t2 = new Thread() {
            public void run() {
                for(int i = 0; i < 50; i++) {
                    if(i == 2) {
                        try {
                            //t1.join(); //插队,加入
                            t1.join(30); //加入,有固定的时间,过了固定时间,继续交替执行
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(getName() + "...bb");
                }
            }


9、多线程-礼让线程:
    yield让出cpu
    
0 0
原创粉丝点击