线程优先级
package com.thread.priority;
/**
* 线程优先级:每个线程都有一个优先级,高优先级的线程执行优先于低优先级线程
* 默认优先级:5
* 范围:1-10
* 这个只有在次数特别多的情况下,效果才可能比较明显
*
* public final int getPriority() 返回线程的优先级。
* public final void setPriority(int newPriority)更改线程的优先级。
*/
public class PriorityDemo {
public static void main(String[] args) {
Priority p = new Priority();
Thread p1 = new Thread(p, "a");
Thread p2 = new Thread(p, "b");
Thread p3 = new Thread(p, "c");
p1.setPriority(10);
p3.setPriority(1);
p1.start();
p2.start();
p3.start();
}
}
package com.thread.priority;
public class Priority implements Runnable {
private int x = 30;
@Override
public void run() {
while (x > 0) {
System.out.println(Thread.currentThread().getName() + "***" + x);
x--;
}
}
}
守护线程以及等待线程
守护线程
package com.thread.priority;
/**
* 假设main线程代表刘备
* public final void setDaemon(boolean on)
* 将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。
* 如果在执行的线程都是守护线程,那么程序就会退出
* 举例:坦克大战
*
*/
public class JoinDemo {
public static void main(String[] args) {
Join j = new Join();
Thread t1 = new Thread(j, "关羽");
Thread t2 = new Thread(j, "张飞");
/**
* 将这两个线程设置为守护线程,守护主线程、
* 当主线程main停止了的时候,两个守护线程也会停止
*/
t1.setDaemon(true);
t2.setDaemon(true);
t1.start();
t2.start();
for (int x = 0; x < 5; x++) {
System.out.println(Thread.currentThread().getName() + "---" + x);
}
}
}
package com.thread.priority;
public class Join implements Runnable {
private int x = 30;
@Override
public void run() {
while (x > 0) {
System.out.println(Thread.currentThread().getName() + "***" + x);
x--;
}
}
}
等待线程
package com.thread.priority;
/**
*
* public final void join(long millis)
* throws InterruptedException等待该线程终止的时间最长为 millis 毫秒。
* 超时为 0 意味着要一直等下去。
* 让其他的线程一直等着。
*
*/
public class JoinDemo {
public static void main(String[] args) throws InterruptedException {
Join j = new Join();
Thread t1 = new Thread(j, "关羽");
Thread t2 = new Thread(j, "张飞");
Thread t3 = new Thread(j, "诸葛亮");
/**
* 此处注意顺序,join在哪些线程前面,哪些线程就得等待join线程结束才可以启动
*/
t3.start();
t3.join();
t1.start();
t2.start();
}
}
package com.thread.priority;
public class Join implements Runnable {
@Override
public void run() {
for (int x = 0; x < 10; x++) {
System.out.println(Thread.currentThread().getName() + "***" + x);
}
}
}
暂停线程
package com.thread.priority;
/**
* public static void yield()暂停当前正在执行的线程对象,并执行其他线程。
* 为了让线程间的执行更和谐一些
* 但是。你想要看到数据的依次出现,还是依赖等待唤醒机制。
*
*/
public class YieldDemo {
public static void main(String[] args) {
Yield y = new Yield();
Thread y1 = new Thread(y);
Thread y2 = new Thread(y);
y1.start();
y2.start();
}
}
package com.thread.priority;
public class Yield implements Runnable {
private int x = 30;
@Override
public void run() {
while (x > 0) {
System.out.println(Thread.currentThread().getName() + "***" + x);
Thread.yield();
x--;
}
}
}
sleep()和wait()方法的区别:
sleep
sleep()是Thread类中的方法,
sleep必须指定参数,
sleep不释放锁对象,
wait()
wait()是Object中的方法
wait可以不用指定时间,可以指定时间
释放所对象
多生产多消费模式
将方法notify()改为notifyAll()
匿名Thread
package com.thread.priority;
public class TestDemo {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 5; i < 10; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}) {
}.start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 10; i < 20; i++) {
System.out.print(i + "");
}
}
}) {
/**
* 为什么会打印此处的?
* 通过查看源码可以知道:
* Thread的run方法的功能是,先判断target是否为null,
* 如果不为null就调用target的run()方法
* 可是我们这里已经重写了run方法,所以,不会执行源码Thread中的run方法,进而不会执行Runnable中 的run方法
* @see java.lang.Thread#run()
*/
@Override
public void run() {
for (int i = 20; i < 30; i++) {
//打印的是此处的
System.out.print(i + " ");
}
}
}.start();
}
}
java与模式 书籍。
大话设计模式
易学设计模式