多线程基础一(java)

这篇博客详细介绍了Java中如何创建和管理线程,包括通过Thread类创建线程,设置和获取线程名称,调整线程优先级,以及使用sleep、join和daemon方法来控制线程的行为。示例代码展示了如何实现多线程同步和进程间通信,对于理解和应用Java多线程编程非常有帮助。
摘要由CSDN通过智能技术生成

Thread类

创建多线程

//重写run方法
package Threading;

public class MyThread extends Thread{
    @Override
    public void run() {
        for(int i =0;i<100;i++){
            System.out.println(i);
        }
    }
}


//测试
package Threading;

public class test {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread();

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

设置和获取线程名称

package Threading;

public class MyThread extends Thread{

    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
//        System.out.println(Thread.currentThread().getName());//获取当前线程名称
//        setName("高铁");
        for(int i =0;i<100;i++){
            System.out.println(getName()+":"+i);
        }
    }
}

//测试
package Threading;

public class test {
    public static void main(String[] args) {
//        MyThread t1 = new MyThread();
//        MyThread t2 = new MyThread();
//        t1.setName("高铁");
//        t2.setName("飞机");
        MyThread t1 = new MyThread("高铁");
        MyThread t2 = new MyThread("飞机");
        System.out.println(Thread.currentThread().getName());//获取当前线程名称
        t1.start();
        t2.start();
    }
}

查看和设置线程优先级

//测试类
package Threading;

public class test {
    public static void main(String[] args) {
//        MyThread t1 = new MyThread();
//        MyThread t2 = new MyThread();
//        t1.setName("高铁");
//        t2.setName("飞机");
        MyThread t1 = new MyThread("高铁");
        MyThread t2 = new MyThread("飞机");
        MyThread t3 = new MyThread("汽车");

//        System.out.println(t1.getPriority());//默认优先级为5
        //设置优先级(1-10)
        t1.setPriority(5);
        t2.setPriority(10);
        t3.setPriority(1);

//        System.out.println(Thread.currentThread().getName());//获取当前线程名称
        t1.start();
        t2.start();
        t3.start();
    }
}

sleep、join、daemon

//sleep
package Threading;

public class MyThread extends Thread{

    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for(int i =0;i<100;i++){
            System.out.println(getName()+":"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//测试
package Threading;

public class test {
    public static void main(String[] args) {

        MyThread t1 = new MyThread("刘备");
        MyThread t2 = new MyThread("孙权");
        MyThread t3 = new MyThread("曹操");

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


//join
package Threading;

public class test {
    public static void main(String[] args) {

        MyThread t1 = new MyThread("康熙");
        MyThread t2 = new MyThread("四阿哥");
        MyThread t3 = new MyThread("八阿哥");

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        t3.start();
    }
}


//setdaemon
package Threading;

public class test {
    public static void main(String[] args) {

        MyThread t1 = new MyThread("关羽");
        MyThread t2 = new MyThread("张飞");

        Thread.currentThread().setName("刘备");
        t1.setDaemon(true);//设为守护进程
        t2.setDaemon(true);//设为守护进程

        t1.start();
        t2.start();
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值