Java 线程

1、线程的概念


/**
 * 现代操作系统都是多任务系统
 * 所谓多任务操作系统即:一台计算机可以同时运行多个程序,而一个程序一般我们将其认为是一个"进程"
 * 线程是比进程跟小的运行单元
 * 如果我们希望一个程序在运行中能够同时处理多个事务,我们就需要使用多线程
 * 
 * 进程的概念:
 * 
 * 
 * 
 */
public class Test1 {
    /*
     * 观察此代码:
     *  1、本程序是一个进程
     *  2、当前进程中存在一个可见的线程,即:main[主线程]
     *  3、当前进程中存在难以察觉的线程也在一定的时候运行,即:垃圾回收
     */

    public static void main(String[] args) {
        String str="OKOK";
        System.out.println(str);
    }
}

2、创建线程

/*
 * 创建线程类,目的用于:创建线程对象
 * 
 * 1、 继承Thread类需要注意:
 * a:Thread类中存在很多已定义方法,我们可以有选择的去重写这些方法
 * b:其中存在一个必须要去重写的方法:run()
 * 
 * 注意:线程启动后,整个程序只能确保在运行过程中,线程会被执行,在运行过程结束前,线程会执行完成
 * 但不能确定线程具体在什么时间点执行
 */
class TTest1 extends Thread{
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("■"+i);
            try{
                this.sleep(10);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
/**
 * java中实现线程的方式[两种]
 * 2、实现Runnable接口
 * 
 * 思考:到底使用方式1合适 还是使用方式2恰当
 * 方式2更能体现面向对象编程的设计[因为java是单根继承体系,如果继承了Thread类则无法进行其他类的拓展]
 */

public class Test3 {
    public static void main(String[] args) {
        Thread t1=new Thread(new ThreadTest1());
        Thread t2=new Thread(new ThreadTest2());
        t1.start();
        t2.start();
    }
}

class ThreadTest1 implements Runnable{
    public void run() {
        for(int i=1;i<=5;i++){
            System.out.print("★ ");
            try{
                Thread.sleep(300);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

3、线程调度


/**
 * 线程调度——优先级
 * 注意:
 *      1、线程的优先级别为:1-10
 *      2、所有线程都是用默认优先级别:5
 *      3、优先级别越高的抢占CPU时间片段的机率越大[理论上]
 * 
 *      三个常量表示常用级别:
 *          Thread.MAX_PRIORITY[10]
 *          Thread.MIN_PRIORITY[1]
 *          Thread.NORM_PRIORITY[5]
 */
public class Test1 {
    public static void main(String[] args) {
        Thread t1=new MyThread1();
        Thread t2=new MyThread2();
        Thread t3=new MyThread3();

        //设置线性调度优先级别
        t1.setPriority(5);//■
        t2.setPriority(5);//◆
        t3.setPriority(6);//★

        System.out.println(t1.getPriority());
        System.out.println(t2.getPriority());
        System.out.println(t3.getPriority());

        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread1 extends Thread{
    public void run(){
        for(int i=1;i<11;i++){
            System.out.println("■"+i);
            try{
                this.sleep(100);//线程休眠100毫秒
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class MyThread2 extends Thread{
    public void run(){
        for(int i=1;i<11;i++){
            System.out.println("◆"+i);
            try{
                this.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class MyThread3 extends Thread{
    public void run(){
        for(int i=1;i<11;i++){
            System.out.println("★"+i);
            try{
                this.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

/**
 * 线程调度——加入线程join()
 *  概念:使一个线程加入另一个线程
 *  现象:一旦B线程被加入当前线程中,那么相当于当前线程被阻塞,等到加入的B线程执行完成后,当前线程才回继续执行
 */
public class Test1 {
    public static void main(String[] args) {
        MyThread1 t1=new MyThread1();
        MyThread2 t2=new MyThread2();
        MyThread3 t3=new MyThread3();

        t3.mt2=t2;

        t1.start();
        t2.start();
        t3.start();

    }
}


class MyThread1 extends Thread{
    public void run(){
        for(int i=1;i<10000;i++){
            System.out.println("■"+i);
            /*try{
                this.sleep(1);
            }catch(InterruptedException e){
                e.printStackTrace();
            }*/
        }
    }
}

class MyThread2 extends Thread{
    public void run(){
        for(int i=1;i<10000;i++){
            System.out.println("◆"+i);
            /*try{
                this.sleep(1);
            }catch(InterruptedException e){
                e.printStackTrace();
            }*/
        }
    }
}

class MyThread3 extends Thread{
    MyThread2 mt2;
    public void run(){
        for(int i=1;i<10000;i++){
            System.out.println("★"+i);
            try{
                //this.sleep(1);
                if(i==4){
                    mt2.join();
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

/**
 * 线程调度——线程让步yield()
 *  概念:让步方法yield()表示,把当前机会放给具有相同优先级别的其他线程
 *  现象:但在实际过程中,很多时候并不会完全让步成功,因为:yield()方法只是让线程进入可运行状态,它还可能去抢到运行机会
 * 
 * 
 * 线程状态:
 *      1、新线程状态
 *      2、可运行状态
 *      3、运行状态
 *      4、等待/阻塞/...状态
 *      5、死线程状态
 * 
 * 注意:死状态的线程,不能再次start()会抛出异常
 * 
 * ◆■★
 */
public class Test1 {
    public static void main(String[] args) {
        MyThread1 t1=new MyThread1();//1、新线程状态
        MyThread2 t2=new MyThread2();
        MyThread3 t3=new MyThread3();

        t1.start();//2、可运行线程状态
        t2.start();
        t3.start();
    }
}


class MyThread1 extends Thread{
    public void run(){
        for(int i=1;i<=10;i++){
            System.out.println("■"+i);//3、运行状态状态
            try{
                this.sleep(5);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class MyThread2 extends Thread{
    public void run(){
        for(int i=1;i<=10;i++){
            System.out.println("◆"+i);
            this.yield();
            try{
                this.sleep(5);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class MyThread3 extends Thread{
    public void run(){
        for(int i=1;i<=10;i++){
            System.out.println("★"+i);
            try{
                this.sleep(5);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值