Java多线程

一:线程和进程
  1. 线程:程序中单独顺序的控制流
    线程本身依靠程序中运行
    线程是程序中顺序控制流,只能使用分配给程序的资源和环境
  2. 进程:执行中的程序
    一个进程可以包含一个或多个线程
    一个进程至少包含一个线程
  3. 单线程:
    程序中只存在一个线程,实际上主方法就是一个主线程
  4. 多线程:
    多线程是在一个程序中运行多个任务
    多线程的目的是更好的使用CPU资源
二:线程的实现
  1. 在Java中,线程的实现有两种
    继承Thread类
    实现Runable接口
  2. Thread类:
    Thread类是在java.lang包中定义的,继承Thread类必须重写run方法
    定义格式:
    class className extends Thread{
    run(){};
    }
    public static void main(String[] args) {
            MyThread t1 = new MyThread("A");
            MyThread t2 = new MyThread("B");
            
            t1.start();
            t2.start();
        }
    public class MyThread extends Thread{
    
        String name;
        public MyThread(String name) {
            this.name = name;
        }
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.println(name + ":" +i);
            }
            super.run();
        }
        
    }
    线程的启动是通过start()方法,并不是通过run方法的。
  3. runable:
        public static void main(String[] args) {
    /*        MyThread t1 = new MyThread("A");
            MyThread t2 = new MyThread("B");*/
            
            MyRunable a1 = new MyRunable("A");
            MyRunable a2 = new MyRunable("B");
            /*t1.start();
            t2.start();*/
            Thread t1 = new Thread(a1);
            Thread t2 = new Thread(a2);
            t1.start();
            t2.start();
        }
public class MyRunable implements Runnable{
    private String name;
    public MyRunable(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(name + ":" + i);
        }
    }
三:线程的状态
创建状态:准备好了一个多线程的对象
就绪状态:调用了start()方法,等待CPU进行调度 运行状态:执行run()方法 阻塞状态:暂时停止执行,可能将资源交给其他线程使用
终止状态:线程销毁
四:线程的常用方法
  1. 取得线程名称
    getName()
  2. 取得线程当前对象
    cunrrentThread()
  3. 判断线程是否启动
    isAlive()
  4. 线程的强行运行
    join()
  5. 线程的休眠
    sleep()
  6. 线程的礼让
    yield()
class RunnableDemo implements Runnable{
    private String name;
    public RunnableDemo(String name) {
        this.name = name;
    }

    @Override
    public void run() {
//        System.out.println("当前线程对象:" + Thread.currentThread().getName());
        for (int i = 0; i < 50; i++) {
                System.out.println(name + ":" + i);
                if (i == 10) {
                    System.out.println("礼让");
                    Thread.yield();
                }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
        }
    }

}

public class ThreadDemo03 {

    public static void main(String[] args) {
        RunnableDemo r1 = new RunnableDemo("A");
        RunnableDemo r2 = new RunnableDemo("B");
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
//        System.out.println(t1.isAlive());
        t1.start();
//        System.out.println(t1.isAlive());
        t2.start();
    }

}
五:多线程的优先级
  1. 优先级顺序设置
    1-MIN_PRIORITY
    10-MAX_PRIORITY
    5-NORM_PRIORITY
    如果什么都不设置默认值为5
    class ThRun implements Runnable{
    
        private String name;
        public ThRun(String name) {
            this.name = name;
        }
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println(name + ":" + i);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    public class ThreadDemo04 {
    
        public static void main(String[] args) {
            Thread t1 = new Thread(new ThRun("A"));
            Thread t2 = new Thread(new ThRun("B"));
            Thread t3 = new Thread(new ThRun("C"));
            t1.setPriority(Thread.MAX_PRIORITY);
            t2.setPriority(Thread.NORM_PRIORITY);
            t3.setPriority(Thread.MIN_PRIORITY);
            t1.start();
            t2.start();
            t3.start();
        }
    
    }
六:线程同步(同步与死锁)
  1. 同步代码块
    在代码块上加上“synchronized”关键字,则此代码块就称为同步代码块
  2. 同步代码块格式
    synchronized(同步对象){
    需要同步的代码块;
    }
  3. 同步方法
    除了代码块可以同步,方法也是可以同步的
  4. 方法同步格式
    synchronized void 方法名称(){}

class MyThreadDemo implements Runnable{

    private int ticket = 5;
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            /*synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("车票:" + ticket--);
                }
            }*/
            tell();
        }
    }
    public synchronized void tell() {
        if (ticket > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("车票:" + ticket--);
        }
    }
}

public class ThreadDemo05 {
    public static void main(String[] args) {
        MyThreadDemo m = new MyThreadDemo();
        Thread t1 = new Thread(m);
        Thread t2 = new Thread(m);
        Thread t3 = new Thread(m);
        t1.start();
        t2.start();
        t3.start();
    }
}

七:线程生命周期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值