【J2SE】java多线程学习笔记

本文详细介绍了Java中多线程的实现方式,包括Runnable接口的使用、线程状态检查、线程休眠、中断、守护线程的设置以及线程优先级的调整。通过实例展示了如何创建和管理线程,以及如何处理并发问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

************************************************************************
****原文:blog.youkuaiyun.com/clark_xu  徐长亮的专栏
************************************************************************
Runnable接口的使用:

//执行

New Thread(runnable class).start()执行 runnable calssrun方法。

publicclass helloimplements Runnable {

    /**

     * @param args

     */

    private Stringname;

    //构造函数

    public hello(){};

    public hello(String name){

        this.name=name;

    }

    //

    publicvoid run() {

        for (int i = 0; i < 3; i++) {

        for(int j=0;j<3;j++){

            System.out.println(name +"运行    " + i + ""+ j +"");

        }

        }

    }

    publicstaticvoid main(String[] args) {

        //TODO Auto-generated method stub

            hello h1=new hello("线程A");

            Thread demo= new Thread(h1);

            hello h2=new hello("线程B");

            Thread demo1=new Thread(h2);

            demo.start();

            demo1.start();

    }

}

 

运行结果:

线程A运行    00

线程B运行    00

线程A运行    01

线程B运行    01

线程A运行    02

线程B运行    02

多个相同的程序代码的线程去处理同一个资源

class my_threadimplements Runnable{

 

    privateintticket = 5; //5张票

 

    publicvoid run() {

        for (int i=0; i<=20; i++) {

            if (this.ticket > 0) {

                System.out.println(Thread.currentThread().getName()+"正在卖票"+this.ticket--);

            }

        }

    }

}

publicclass MyThread {

     

    publicstaticvoid main(String [] args) {

        my_thread my = new my_thread();

        new Thread(my,"1号窗口").start();

        new Thread(my,"2号窗口").start();

        new Thread(my,"3号窗口").start();

    }

}

查看线程运行状态

Thread demo = new Thread(he);

System.out.println("线程启动之前---" + demo.isAlive());            

demo.start();

System.out.println("线程启动之后---" + demo.isAlive());

线程的休眠

class helloimplements Runnable {

    publicvoid run() {

        for (int i = 0; i < 3; i++) {

            try {

                Thread.sleep(2000);

            } catch (Exception e) {

                e.printStackTrace();

            }

            System.out.println(Thread.currentThread().getName() + i);

        }

    }

 

    publicstaticvoid main(String[] args) {

        hello he = new hello();

        new Thread(he,"线程").start();

       

    }

}

线程中断

class helloimplements Runnable {

    publicvoid run() {

        System.out.println("执行run方法");

        try {

            Thread.sleep(10000);

            System.out.println("线程完成休眠");

        } catch (Exception e) {

            System.out.println("休眠被打断");

            return//返回到程序的调用处

        }

        System.out.println("线程正常终止");

    }

 

    publicstaticvoid main(String[] args) {

        hello he = new hello();

        Thread demo = new Thread(he,"线程");

        demo.start();

        try{

            Thread.sleep(2000);

        }catch (Exception e) {

            e.printStackTrace();

        }

        demo.interrupt(); //2s后中断线程

    }

}

注:

启动进程(进程执行1秒)

进程休眠2秒。

同时多线程要求进程中断

守护进程

定义:守护线程--也称“服务线程”,在没有用户线程可服务时会自动离开。优先级:守护线程的优先级比较低,用于为系统中的其它对象和线程提供服务。

设置:通过setDaemon(true)来设置线程为“守护线程”;将一个用户线程设置为守护线程的方式是在 线程对象创建 之前 用线程对象的setDaemon方法。

         class helloimplements Runnable {

    publicvoid run() {

        while (true) {

            System.out.println(Thread.currentThread().getName() +"在运行");

        }

    }

 

    publicstaticvoid main(String[] args) {

        hello he = new hello();

        Thread demo = new Thread(he,"线程");

        demo.setDaemon(true);

        demo.start();

    }

}

优先级设置

class my_threadimplements Runnable{

 

    privateintticket = 5; //5张票

 

    publicvoid run() {

        for (int i=0; i<=20; i++) {

            if (this.ticket > 0) {

                System.out.println(Thread.currentThread().getName()+"正在卖票"+this.ticket--);

            }

        }

    }

}

publicclass MyThread {

     

    publicstaticvoid main(String [] args) {

        my_thread my = new my_thread();

        Thread h1=new Thread(my,"1号窗口");

        Thread h2=new Thread(my,"2号窗口");

        Thread h3=new Thread(my,"3号窗口");

        h1.setPriority(5);

        h2.setPriority(7);

        h3.setPriority(8);

        h1.start();

        h2.start();

        h3.start();

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值