观测线程状态
//观察测试线程的状态
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("/");
});
//观察状态
Thread.State state=thread.getState();
System.out.println(state);//NEW
//观察启动后
thread.start();//启动线程
state=thread.getState();
System.out.println(state);//Run
while (state!=Thread.State.TERMINATED){//只要线程不终止,就一直输出
Thread.sleep(100);
state=thread.getState();//更新线程状态
System.out.println(state);
}
}
}
测试线程优先级
//测试线程的优先级
public class TestPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
MyPriority myPriority=new MyPriority();
Thread t1=new Thread(myPriority);
Thread t2=new Thread(myPriority);
Thread t3=new Thread(myPriority);
Thread t4=new Thread(myPriority);
t1.start();
//先设置优先级,再启动
t2.setPriority(2);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10
t4.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
}
}
守护线程
//测试守护线程
//徐家守护你
public class TestDaemon {
public static void main(String[] args) {
Xiao xiao=new Xiao();
Nian nian=new Nian();
Thread thread=new Thread(xiao);
thread.setDaemon(true);//默认时false表示时用户线程,正常的线程都是用户线程
thread.start();//徐晓启动了
new Thread(nian).start();//凤年 用户线程启动
}
}
class Xiao implements Runnable{
@Override
public void run() {
while (true){
System.out.println("徐晓一直守护你");
}
}
}
class Nian implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("你活的非常累!!");
}
System.out.println("还是要byebye啦");
}
}
同步方法同步块
//不安全的取钱
public class UnsafeBank {
public static void main(String[] args) {
Account account=new Account(1000,"恋爱基金");
Drawing drawing=new Drawing(account,50,"你");
Drawing drawing2=new Drawing(account,100,"你女票");
drawing.start();
drawing2.start();
}
}
class Account{
int money;
String name;
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
class Drawing extends Thread{
Account account;
int drawMoney;
int nowMonry;
public Drawing(Account account, int drawMoney,String name) {
super(name);
this.account = account;
this.drawMoney = drawMoney;
}
@Override
public void run() {
synchronized (account){
if(account.money-drawMoney<0){
System.out.println(Thread.currentThread().getName()+"余额不足,取款失败");
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.money=account.money-drawMoney;
nowMonry=nowMonry+drawMoney;
System.out.println("余额位:"+account.money);
System.out.println(this.getName()+"手上有"+nowMonry);
}
}
}
测试JUC安全类型的集合
//测试JUC安全类型的集合
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list=new CopyOnWriteArrayList<>();
for (int i = 0; i < 1000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
死锁
//死锁:多个线程互相抱着对方需要的资源,然后形成僵持
public class DeadLock {
public static void main(String[] args) {
Makeup makeup=new Makeup(0,"黄鸡凯");
Makeup makeup1=new Makeup(1,"陆某");
makeup.start();
makeup1.start();
}
}
//口红
class Lipsttick{
}
//镜子
class Mirror{
}
//
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipsttick lipsttick=new Lipsttick();
static Mirror mirror=new Mirror();
int choice;//选择
String gireName;//收用化妆品的人
Makeup(int choice,String gireName){
this.choice=choice;
this.gireName=gireName;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//化妆:互相持有对方的锁,就是需要拿到对方的资源
private void makeup() throws InterruptedException {
if (choice==0){
synchronized (lipsttick){//获得口红的锁
System.out.println(this.gireName+"获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror){//一秒钟后获得镜子
System.out.println(this.gireName+"获得镜子的锁");
}
}else {
synchronized (mirror){//获得镜子
System.out.println(this.gireName+"获得镜子的锁");
Thread.sleep(2000);
}
synchronized (lipsttick){//两秒后获得口红的锁
System.out.println(this.gireName+"获得口红的锁");
}
}
}
}
锁
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2=new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable{
int ticketNum=10;
//定义lock锁
private ReentrantLock lock=new ReentrantLock();
@Override
public void run() {
while (true){
lock.lock();//加锁
try {
if(ticketNum>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNum--);
}else {
break;
}
}finally {
//解锁
lock.unlock();
}
}
}
}
生产者消费者问题
管程法
//测试:生产这消费者模型--》利用缓冲区解决:管程法
//生产者,消费者,产品,缓冲区
public class TestPc {
public static void main(String[] args) {
SyContainer container=new SyContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
//生产者
class Productor extends Thread{
SyContainer container;
public Productor(SyContainer container){
this.container=container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了:"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
//消费者
class Consumer extends Thread{
SyContainer container;
public Consumer(SyContainer container){
this.container=container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了第"+container.pop().id+"只鸡");
}
}
}
//产品
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
//缓冲区
class SyContainer{
//需要一个容器大小
Chicken[] chickens=new Chicken[10];
int count=0;
//生产者放入
public synchronized void push(Chicken chicken){
//如果容器满了,就需要等待消费者消费
if(count==chickens.length){
//通知消费者消费,身长等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,我们就需要丢入产品
chickens[count] =chicken;
count++;
//可以通知消费者消费了
this.notifyAll();
}
//消费者消费产品
public synchronized Chicken pop(){
//判断能否消费
if (count == 0) {
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消费
count--;
Chicken chicken=chickens[count];
//吃完了,通知生产者生产
this.notifyAll();
return chicken;
}
}
信号灯法
//测试生产者消费者问题2:信号灯法,标志位解决
public class TestPC2 {
public static void main(String[] args) {
TV tv=new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者--》演员
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv=tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if(i%2==0){
this.tv.play("熊出没正在播放");
}else {
this.tv.play("宝宝巴士:我会自己上厕所");
}
}
}
}
//消费者--》观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv=tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品--》节目
class TV{
//演员表演,观众等待
//观众观看,演员等待
String voice;//表演的节目
boolean flag=true;
//表演
public synchronized void play(String voice){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了"+voice);
//通知观众观看
this.notifyAll();
this.voice=voice;
this.flag=!this.flag;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观看了:"+voice);
this.notifyAll();
this.flag=!this.flag;
}
}
线程池
//测试线程池
public class TestPool {
public static void main(String[] args) {
//1.创建服务,创建线程池
//newFixedThreadPool 参数为:线程池大小
ExecutorService service= Executors.newFixedThreadPool(10);
//执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//2.关闭线程池
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
}









