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();
}
}
}
}
//线程基础
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();
}
}
}
}