JAVA线程应用

sleep方法


class MyThread implements Runnable{

@Override
public void run() {
    for(int i=1;i<=15;i++){
        System.out.println(Thread.currentThread().getName()+"执行第"+i+"次");
        try {
            Thread.sleep(1000);//参数为毫秒,休眠时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}

}

public class SleepDemo {

public static void main(String[] args) {
    MyThread mt = new MyThread();
    Thread t = new Thread(mt);
    t.start();
    Thread t1 = new Thread(mt);
    t1.start();
}

}

join方法


class MyThread extends Thread{

    public void run(){
    for(int i=1;i<=10;i++){
    System.out.println(getName()+"正在执行"+i+"次");
    }
}

}

public class JoinDemo {

public static void main(String[] args){
    MyThread mt = new MyThread();
    mt.start();
    try {
        mt.join();//join方法抢占资源,它执行完其他才能执行
        //mt.join(10)该参数时间有效期过了之后,不管该线程有没有执行完,都不再继续执行
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(int i=1;i<=10;i++){
        
    System.out.println("主线程运行第"+i+"次");
    }
    
}

}

线程优先级


class MyThread extends Thread{

private String name;
public MyThread(String name){
    this.name=name;
}

public void run(){
    for(int i=1;i<=10;i++){
        System.out.println("线程"+name+"正在运行"+i);
    }
}

}

public class PriorityDemo {

public static void main(String[] args){
    int mainPriority = Thread.currentThread().getPriority();
    System.out.println("主线程的优先级:"+mainPriority);
    MyThread mt1 =new MyThread("线程1");
    MyThread mt2 =new MyThread("线程2");
    //mt1.setPriority(10);
    mt1.setPriority(Thread.MAX_PRIORITY);
    mt2.setPriority(Thread.MIN_PRIORITY);
    mt2.start();//优先级与操作系统,cpu等等有关
    mt1.start();//      System.out.println("线程1的优先级为:"+mt1.getPriority());//        System.out.println("线程2的优先级为:"+mt2.getPriority());
}

}

线程同步

image.png

public class Bank {

private String account;
private int balance;
public Bank(String account, int balance) {
    super();
    this.account = account;
    this.balance = balance;
}
public String getAccount() {
    return account;
}
public void setAccount(String account) {
    this.account = account;
}
public int getBalance() {
    return balance;
}
public void setBalance(int balance) {
    this.balance = balance;
}
@Override
public String toString() {
    return "Bank [account=" + account + ", balance=" + balance + "]";
}


public void saveAccount(){
    synchronized(this){
    int balance=getBalance();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    balance+=100;
    setBalance(balance);
    System.out.println("存款后的账户余额为:"+balance);
    }
}

public void drawAccount(){
    synchronized(this){
    int balance=getBalance();
    balance=balance-200;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    setBalance(balance);
    System.out.println("取款后账户余额:"+balance);
}
}

}

public class SaveAccount implements Runnable{

Bank bank;
public SaveAccount(Bank bank){
    this.bank=bank;
}

public void run(){
    bank.saveAccount();
}

}

public class DrawAccount implements Runnable{

Bank bank;
public DrawAccount(Bank bank){
    this.bank=bank;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    bank.drawAccount();
}

}


public class Test {

public static void main(String[] args) {
    Bank bank= new Bank("1001",1000);
    SaveAccount sa = new SaveAccount(bank);
    DrawAccount da = new DrawAccount(bank);
    Thread save = new Thread(sa);
    Thread draw = new Thread(da);
    save.start();
    draw.start();
    try {
        save.join();
        draw.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(bank);

}

}

线程间通信


以生产者setN与消费者getN为例


image.png

public class Queue {

private int n;
boolean flag = false;

public int getN() {
    if(!flag){
        try {
            wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.out.println("消费:"+n);
    flag=false;//消费完毕,容器中没有数据
    notifyAll();
    return n;
}

public void setN(int n) {
    if(flag){
        try {
            wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    this.n = n;
    flag=true;//生产完毕,容器中已经有数据
    System.out.println("生产:"+n);
    notifyAll();
}

}

public class Producer implements Runnable{

Queue queue;
Producer(Queue queue){
    this.queue=queue;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    int i=0;
    while(true){
        queue.setN(i++);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

}

public class Consumer implements Runnable{

Queue queue;
Consumer(Queue queue){
    this.queue=queue;
}
@Override
public void run() {
    // TODO Auto-generated method stub
    while(true){
        queue.getN();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

}


public class Test {

public static void main(String[] args) {
    Queue queue = new Queue();
    new Thread(new Producer(queue)).start();
    new Thread(new Consumer(queue)).start();
}

}

你可能感兴趣的:(JAVA线程应用)