线程

2020-07-28  本文已影响0人  MHLEVEL

Thread 属于 java.lang包,所以可以直接用,不需要引入

public static void printSlowly(String text, long interval) throws InterruptedException {
        for (char ch : text.toCharArray()) {
            Thread.sleep(interval);
            System.out.print(ch + " ");
        }
        System.out.println();
    }
System.out.println("程序开始,执行的线程名字叫做" + Thread.currentThread().getName());

创建线程的方式


public static void main(String[] args){
        Thread thread = new Thread(new PrintStoryRunnable("pilibala", 300));
}
static class PrintStoryRunnable implements Runnable {
        private String text;
        private long interval;

        public PrintStoryRunnable(String text, long interval) {
            this.text = text;
            this.interval = interval;
        }

        @Override
        public void run() {
            try {
                double num = Math.random();
                System.out.println("执行这段代码的线程名字叫做" + Thread.currentThread().getName());
                printSlowly(text, interval);
                System.out.println(Thread.currentThread().getName() + "执行结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

同步控制之synchronized

public synchronized void change(){
        //balabala
}
// 只要不是 null ,任何一个对象都可以用来做锁
private Object lockObj = new Object();
synchronized (lockObj){
            number += delta;
}
public synchronized static void changedata(){
        // method body
}

// synchronized 修饰静态方法的效果等同于用 synchronized 锁住当前类
// 例如当前类的类名是 DataHolder

synchronized (DataHolder.class){
        // balabala
}

守护线程

// 设置守护线程的方法就是在线程调用start() 方法前调用 setDeamon(true)

wait 和 notify 用于多线程协调运行

join : Waits for this thread to die.

/**
     * Waits for this thread to die.
     *
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final void join() throws InterruptedException {
        join(0);
    }

public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

java.lang.Thread.join(long millis, int nanos)方法实例

ThreadLocal 是个什么东西?

volatile

上一篇下一篇

猜你喜欢

热点阅读