本文总结java中多线程
1、线程的创建
package com.wllfengshu.test;
import org.junit.Test;
/**
* 线程的创建Thread
* 线程四种状态:新建、运行、中断、死亡
*/
public class Test1线程创建 {
//线程创建
@Test
public void test1(){
A a=new A();
Thread b=new Thread(new B());
a.start();
b.start();
while(true){//主线程也在死循环
System.out.println("Main");
}
}
//线程常用方法
@Test
public void test2(){
A a=new A();
System.out.println(a.isAlive());//线程是否在运行状态
a.start();//开启线程
System.out.println(a.getName());//获取线程名族
while(true){//主线程也在死循环
System.out.println("Main");
try {
Thread.sleep(1000);//睡觉
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("此时占用CUP的线程是:"+Thread.currentThread());//此时占用CUP的线程
}
}
}
class A extends Thread{//继承Thread类
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);//睡觉
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" A ");
}
}
}
class B implements Runnable{//实现Runnable接口
@Override
public void run() {
while(true){
System.out.println(" B ");
}
}
}
2、线程同步
package com.wllfengshu.test;
import org.junit.Test;
public class Test2线程同步 {
@Test
public void test1(){
Bank bank=new Bank();
Thread kuaiji=new Thread(bank);
kuaiji.setName("会计");
Thread chuna=new Thread(bank);
chuna.setName("出纳");
kuaiji.start();
chuna.start();
}
}
class Bank implements Runnable{
private int money=0;
public synchronized void addOrSub(int n){
if (Thread.currentThread().getName().equals("会计")) {
for (int i = 0; i < 3; i++) {
money+=n;
System.out.println(Thread.currentThread().getName()+"做了加法运算"+money+",睡觉了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else if (Thread.currentThread().getName().equals("出纳")){
for (int i = 0; i < 3; i++) {
money-=n;
System.out.println(Thread.currentThread().getName()+"做了减法运算"+money+",睡觉了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("会计")) {
addOrSub(8);//加
}else if (Thread.currentThread().getName().equals("出纳")){
addOrSub(5);//减
}
}
}
3、Wait和notify
package com.wllfengshu.test;
public class Test3Wait和notify {
//售票员没有零钱 每张票5元
//张一用一张10的去买票,等
//张二用一张5元的买票,问题解决
public static void main(String[] args) {
Tick tick=new Tick();
Thread zhangOne= new Thread(tick);
zhangOne.setName("张一");
Thread zhangTwo= new Thread(tick);
zhangTwo.setName("张二");
zhangTwo.start();
zhangOne.start();
}
}
class Tick implements Runnable{
private int saler=0;//售票员
public synchronized void saleTick(int money){
if (money==5) {
System.out.println(Thread.currentThread().getName()+"的钱正好,给他卖票了");
saler+=5;
}else if(money==10){
while(saler<5){
try {
System.out.println(Thread.currentThread().getName()+"给的10元,不能买票,等待");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
saler+=10;
System.out.println(Thread.currentThread().getName()+"给他卖票了");
}
notifyAll();
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("张一")) {
saleTick(5);
}else if (Thread.currentThread().getName().equals("张二")){
saleTick(10);
}
}
}