创建线程

package test2;


//线程基础
public class ThreadTest {
public static void main(String[] args) {
Thread a = new Thread(new ThreadA());
ThreadB b = new ThreadB();
// 设置线程的优先级,默认为5,最大是10
b.setPriority(9);
b.setName("线程b");
// 设置b为守护 线程,当所有前台线程结束后,守护线程也结束(b线程不会循环10000次)
b.setDaemon(true);
Thread d = new Thread(new Runnable() {


@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(i + " 线程名: d");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
Thread c = new Thread("线程c") {
@Override
public void run() {
super.run();
for (int i = 0; i < 100; i++) {
System.out.println(i + " 线程名: " + this.getName() + " 优先级: " + this.getPriority());// this指当前线程
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
a.start();
b.start();
c.start();
d.start();
}
}


class ThreadA implements Runnable {


@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(i + "  线程c ");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


class ThreadB extends Thread {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(i + " 线程名: " + this.getName() + " 优先级: " + this.getPriority());// this指当前线程
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值