设置线程的优先级: 可以设置线程的优先级,线程优先级一般是1—10级
public class MyTest {
public static void main(String[] args) {
//Java 采用的是抢占式调度模型,多个线程优先级相同,采用随机的方式,去执行
MyThread th1 = new MyThread();
th1.setPriority(Thread.MAX_PRIORITY);//优先级最高
th2.setPriority(Thread.MIN_PRIORITY);//优先级最低
th3.setPriority(Thread.NORM_PRIORITY);//优先级为5
//获取线程的优先级
int priority = th1.getPriority();
th1.start();
th2.start();
th3.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+"=="+i);
}
}
}
线程休眠:使线程处于休眠状态,休眠时间到,则继续执行线程;
public class MyTest {
public static void main(String[] args) throws InterruptedException {
System.out.println("主线程");
System.out.println("再等两秒钟去开启子线程");
//使当前线程休眠一定的时间量,必须传入时间量,不释放锁;
Thread.sleep(5000);
MyThread myThread = new MyThread("子线程");
myThread.start();
}
}
class MyThread extends Thread{
//子线程构造;
public MyThread(String name) {
super(name);
}
@Override
public void run() {
try {
//是当前线程休眠单位毫秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
//输出子线程名字和循环变量;
System.out.println(this.getName()+"=="+i);
}
}
}
加入线程:join(),等待当前线程结束后,才可以执行其他线程;
即线程的并发执行,转换为顺序执行;
注意:在线程开启之后调用。
public class MyTest {
public static void main(String[] args) throws InterruptedException {
//按顺序执行三个线程任务;
MyThread myThread1 = new MyThread("th1");
MyThread myThread2 = new MyThread("th2");
MyThread myThread3 = new MyThread("th3");
myThread1.start();
myThread1.join();
myThread2.start();
myThread2.join();
myThread3.start();
}
}
class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+"=="+i);
}
}
}
线程的礼让:线程礼让效果并不明显,因为礼让时,其暂停的时间非常短,
当礼让时间结束之后,其他线程还没有进来,此时当前线程就会和其他线程争抢时间片;
public class MyTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread1 = new MyThread("th1");
MyThread myThread2 = new MyThread("th2");
myThread1.start();
myThread2.start();
}
}
class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//线程礼让
Thread.yield();
System.out.println(this.getName()+"=="+i);
}
}
}
线程守护: 设置该线程为守护线程,该方法必须在启动线程前调用。
setDaemon(true);
public class MyTest {
public static void main(String[] args) throws InterruptedException {
//当前的主线程为用户线程
Thread.currentThread().setName("用户线程");
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}
//当用户线程执行完毕,那么守护线程就得马上死亡
MyThread myThread2 = new MyThread("守护线程1:");
MyThread myThread3 = new MyThread("守护线程2:");
//设置该线程为守护线程,该方法必须在启动线程前调用。
myThread2.setDaemon(true);
myThread3.setDaemon(true);
myThread2.start();
myThread3.start();
}
}
class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 4; i++) {
System.out.println(this.getName()+"=="+i);
}
}
}
打断线程的一个阻塞状态:interrupt()方法,简单实例如下:
其实就是使得等待的或者休眠的线程停止当前的状态,继续执行;
public class MyTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread1 = new MyThread("th1");
myThread1.start();
//休眠2秒;
Thread.sleep(2000);
//打断线程的一个阻塞状态
myThread1.interrupt();
}
}
class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
@Override
public void run() {
try {
//休眠的方法,其实就是让线程处于了一种阻塞状态
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+"=="+i);
}
}
}