JAVA 线程学习

image.png
package threadProj;

public class Letter implements Runnable {
    private char[] letter = new char[26];
    public Letter() {
        int j =0;
        for(char i = 'a'; i<='z' ; i++) {
            letter[j++] = i;
        }
    }
    
    @Override
    public void run() {
        for(char n: letter) {
            System.out.println(n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
    
}

package threadProj;

public class ThreadTest {

    public static void main(String[] args) {
        Letter letterRunnable = new Letter();
        Thread r1 = new Thread(letterRunnable);
        r1.start();
    }

}

  1. Thread.currentThread()
    优先级: thread.currentThread().getPriority();
    mt1. setPriority(Thread.MAX_PRIORITY);

  2. 线程同步

package indi.yuan.wheather;

public class Wheather {
    private int temperature;
    private int humidity;
    public boolean canRead = false;

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }

    public synchronized void generate() {
        if(canRead) {
            try {
//              暂停线程
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int temp = (int) (Math.random() * 40);
        int humi = (int) (Math.random() * 100);

        this.setHumidity(humi);
        this.setTemperature(temp);
        
        canRead = true;
//      唤起线程
        notifyAll();

    }

    public synchronized void read() {
        if(!canRead) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(" 温度" + this.temperature + "湿度 " + this.humidity);
        canRead = false;
    }

    public String toString() {
        return "读取天气方法";
    }

}

package indi.yuan.wheather;

public class GenerateWeather implements Runnable {
    Wheather wheather;
    GenerateWeather(Wheather wheather){
        this.wheather = wheather;
    }
    @Override
    public void run() {
        while(true) {
            wheather.generate();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

}

package indi.yuan.wheather;

public class ReadWeather implements Runnable {
    Wheather wheather;
    ReadWeather(Wheather wheather){
        this.wheather = wheather;
    }
    @Override
    public void run() {
        while(true) {
            wheather.read();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

}
package indi.yuan.wheather;

public class WheatherTest {

    public static void main(String[] args) {
        Wheather wheather = new Wheather();
        new Thread(new GenerateWeather(wheather)).start();
        new Thread(new ReadWeather(wheather)).start();
    }

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java多线程学习 [-] 一扩展javalangThread类 二实现javalangRunnable接口 三T...
    影驰阅读 2,987评论 1 18
  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 2,493评论 1 15
  • 线程基础 1. 线程的生命周期 1.1 新建状态: 使用 new 关键字和 Thread 类或其子类建立一个线程对...
    开始以后_阅读 146评论 0 3
  • ackageThreadDemo;/** * 多线程 * 程序:一组指定的集合(没有运行的程序) * 进程...
    java大哥阅读 329评论 0 0
  • 进程和线程 进程 所有运行中的任务通常对应一个进程,当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中...
    胜浩_ae28阅读 5,142评论 0 23