java 多线程学习2 2021/09/04
线程停止、线程休眠、线程礼让、线程强制执行
观察线程状态
- 线程有五个状态:
NEW/RUNNABLE/TIME_WAITING/TERMINATED
- 用Thread.State可以观察到
public class TestThreadState {
public static void main(String[] args) {
Thread th = new Thread(new MyThread());
Thread.State state=th.getState();
System.out.println(state);
th.start();
while(state!=Thread.State.TERMINATED){
try {
th.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
state = th.getState();
}
System.out.println(state);
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println(Thread.currentThread().getState());
}
}
}
线程优先级
- 优先级高,只是被执行的概率高,优先级高->低 对应是 10–>1
public class TestThreadState {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+
Thread.currentThread().getPriority()+"执行");
MyThread t=new MyThread();
Thread th1=new Thread(t,"线程1");
Thread th2=new Thread(t,"线程2");
Thread th3=new Thread(t,"线程3");
th1.setPriority(1);
th2.setPriority(2);
th3.setPriority(3);
th1.start();
th2.start();
th3.start();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+
Thread.currentThread().getPriority()+"执行");
}
}
main-->5执行
线程3-->3执行
线程1-->1执行
线程2-->2执行
线程分为用户线程和守护线程
- 主线程是用户线程,必须执行完
- 垃圾回收。。是守护线程,可以不执行完
线程同步【重点】
public class Application {
public static void main(String[] args) {
Person p1 = new Person("丈夫", 80);
Person p2 = new Person("妻子", 50);
Drawing drawing1=new Drawing(p1);
Drawing drawing2=new Drawing(p2);
new Thread(drawing1).start();
new Thread(drawing2).start();
}
}
class Person{
private String name;
private int drawMoney;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDrawMoney() {
return drawMoney;
}
public void setDrawMoney(int drawMoney) {
this.drawMoney = drawMoney;
}
public Person(String name, int drawMoney) {
this.name = name;
this.drawMoney = drawMoney;
}
}
class Drawing implements Runnable{
public static int money=100;
Person person;
public Drawing(Person person) {
this.person = person;
}
@Override
public void run() {
drow(person);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void drow(Person person) {
if (money - person.getDrawMoney() >= 0) {
money-=person.getDrawMoney();
System.out.println(person.getName()+"取了"+person.getDrawMoney()+" 余额:"+money);
}
}
}
丈夫取了80 余额:-30
妻子取了50 余额:-30
public class MyThread {
public static void main(String[] args) {
Th th=new Th();
for (int i = 0; i < 1000; i++) {
new Thread(th).start();
}
System.out.println(th.list.size());
}
}
class Th implements Runnable{
ArrayList<Integer> list=new ArrayList<>(1000);
@Override
public void run() {
list.add(1);
}
}
433
public class Application {
public static void main(String[] args) {
Person p1 = new Person("丈夫", 80);
Person p2 = new Person("妻子", 50);
Drawing drawing1=new Drawing(p1);
Drawing drawing2=new Drawing(p2);
new Thread(drawing1).start();
new Thread(drawing2).start();
}
}
class Person{
private String name;
private int drawMoney;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDrawMoney() {
return drawMoney;
}
public void setDrawMoney(int drawMoney) {
this.drawMoney = drawMoney;
}
public Person(String name, int drawMoney) {
this.name = name;
this.drawMoney = drawMoney;
}
}
class Drawing implements Runnable{
public static int money=100;
Person person;
public Drawing(Person person) {
this.person = person;
}
@Override
public void run() {
synchronized ((Integer)money){
drow(person);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void drow(Person person) {
if (money - person.getDrawMoney() >= 0) {
money-=person.getDrawMoney();
System.out.println(person.getName()+"取了"+person.getDrawMoney()+" 余额:"+money);
}
}
}
丈夫取了80 余额:20
----------------------------------------------------------
public class Account {
private int Money;
public Account(int money) {
Money = money;
}
public int getMoney() {
return Money;
}
public void setMoney(int money) {
Money = money;
}
}
public class Person {
private String name;
private int drawMoney;
public Person(String name, int drawMoney) {
this.name = name;
this.drawMoney = drawMoney;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDrawMoney() {
return drawMoney;
}
public void setDrawMoney(int drawMoney) {
this.drawMoney = drawMoney;
}
}
public class Bank implements Runnable{
private static Account account;
private static Person person;
public Bank(Account account, Person person) {
this.account = account;
this.person = person;
}
@Override
public void run() {
synchronized (account) {
this.draw();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void draw() {
if (account.getMoney() - person.getDrawMoney() < 0) {
System.out.println("钱不够了,取不了");
} else {
System.out.println(person.getName()+"取了"+person.getDrawMoney()+"万元"+
" 余额:"+(account.getMoney()-person.getDrawMoney()));
account.setMoney(account.getMoney()-person.getDrawMoney());
}
}
}
public class Application {
public static void main(String[] args) {
Account account=new Account(100);
Person p1 = new Person("老公", 80);
Person p2 = new Person("妻子", 50);
Bank bank1=new Bank(account,p1);
Bank bank2=new Bank(account,p2);
new Thread(bank1).start();
new Thread(bank2).start();
}
}
妻子取了50万元 余额:50
妻子取了50万元 余额:0
public class application {
public static void main(String[] args) {
BuyTicket th1 = new BuyTicket();
new Thread(th1,"线程1").start();
new Thread(th1,"线程2").start();
new Thread(th1,"线程3").start();
}
}
class BuyTicket implements Runnable {
private final ReentrantLock lock = new ReentrantLock();
public static int ticketNum=10;
@Override
public void run() {
while(true) {
try {
lock.lock();
if (ticketNum > 0) {
System.out.println(Thread.currentThread().getName()+"拿了第"
+ticketNum--+"张票");
}else{
break;
}
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
}
线程协作【线程通信】
线程池