Java多线程

1.Java创建一个线程

- Thread 类 
  线程类(Thread)包含一个可以运行的过程(方法):run()方法
- 创建一个具体线程的步骤如下:
  第一,继承Thread类
  第二,覆盖run方法(就是更新运行过程),实现用户自己的过程 
  第三,创建线程实例(就是创建一个线程)
  第四,使用线程实例的start()方法启劢线程, 启劢以后线程会尽快的去并发执行run()

2.线程的状态

线程的5种状态

- New          新建状态
- Runnable     可运行(就绪)状态
- Running      运行(正在运行)状态
- Block        阻塞(挂起)状态
- Dead         死亡状态

3.线程状态管理

- 让出CPU Thread.yield()
  当前线程让出处理器(离开 Running 状态),
  使当前线程迚入Runnable状态等待
- 休眠 Thread.sleep(times)
  使当前线程从Running放弃处理器迚入Block状态, 
  休眠times毫秒,再返回到Runnable 
  如果其他线程打断当前线程的Block(sleep), 
  就会发生InterruptedException。

4.线程的常用属性及方法

- 线程的优先级(资源紧张时候,尽可能优先)
  t3.setPriority(Thread.MAX_PRIORITY); 设置为最高优先级
  默认有10优先级,优先级高的线程获得执行(迚入Running状态)的机会多
  默认的优先级是 5
- 后台线程(守护线程,精灵线程)
  t1.setDaemon(true);
  Java迚程的结束:当前所有前台线程都结束时,Java迚程结束
  当前台线程结束时,不管后台线程是否结, 都要被停掉!
- 获得线程名字 
  getName()
- 获得当前线程
  Thread main = Thread.currentThread();
public class ThreadDemo {
    public static void main(String[] args) {
        Person1 p1 = new Person1();
        Person2 p2 = new Person2();
        Person3 p3 = new Person3();
        p3.setPriority(Thread.MAX_PRIORITY);
        p1.setPriority(Thread.MIN_PRIORITY);
        p2.setDaemon(true);
        p1.start();
        p2.start();
        p3.start();
        System.out.println("main over!");
    }
}
class Person1 extends Thread {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("你是谁呀?");
            Thread.yield();
        }
        System.out.println("你是谁呀?over!");
    }
}
class Person2 extends Thread {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("修水管的!");
            Thread.yield();
        }
        System.out.println("修水管的!over!");
    }
}
class Person3 extends Thread {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("弹弓!");
            Thread.yield();
        }
        System.out.println("弹弓!over!");
    }
}

5.两种方式创建线程

public class ThreadInitDemo { //线程的创建方式
    public static void main(String[] args) {
        //使用匿名内部类创建线程
        Thread t1 = new Thread() {
            public void run() {
                System.out.println("1111111111");
            }
        };
        t1.start();
        //使用Runnable接口创建线程
        Runnable runner = new Runnable() {
            public void run() {
                System.out.println("22222222222222");
            }
        };
        //将Runnable实例作为构造参数
        Thread t2 = new Thread(runner);
        t2.start();
        //使用Runnable接口创建匿名类,创建线程实例
        Thread t3 = new Thread(new Runnable() {
            public void run() {
                System.out.println("333333333");
            }
        });
        t3.start();
        //创建匿名类实例,直接启动线程
        new Thread() {
            public void run() {
                System.out.println("4444444444");
            }
        }.start();
        //创建匿名类实例,使用Runnable接口
        new Thread(new Runnable() {
            public void run() {
                System.out.println("555555555555");
            }
        }).start(); 
    }
}

6.Sleep状态的打断唤醒

- Thread.sleep(times)
  使当前线程从Running状态放弃处理器,进入Block状态,休眠times(单位为毫秒), 
  休眠结束后,再返回到Runnable状态
- interrupt()方法打断/中断
  使用该方法可以让一个线程提前唤醒另外一个sleep Block的线程
- 被唤醒线程会出现中断异常

线程休眠

public class SleepDemo {
    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                long end = System.currentTimeMillis();
                System.out.println("线程休眠了:"+(end - start));
                System.out.println("线程结束");
            }
        }.start();
        System.out.println("main结束");
    }
}

线程休眠打断

public class SleepDemo2 {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                long start = System.currentTimeMillis();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {}
                long end = System.currentTimeMillis();
                System.out.println("线程休眠了:"+(end - start));
                System.out.println("线程结束");
            };
        };
        t.start();
        try {
            Thread.sleep(3000);
            t.interrupt();
        } catch (InterruptedException e) {}
        System.out.println("main结束");
    }
}

线程休眠和中断

public class SleepDemo3 {
    public static void main(String[] args) {
        final Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("去睡觉了!");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("干嘛啊?破相了!");
                        break;
                    }
                }
            };
        };
        t1.start();
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 12; i++) {
                    System.out.println("砸墙,哐当!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.err.println("终于砸穿了!");
                t1.interrupt();
            }
        };
        t2.start();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值