Java基础

Java 常用工具类--多线程

2018-09-16  本文已影响0人  磊_5d71

System.out.println快捷键 sysout option+/

通过Thread类创建线程

package com.alan.thread;


class MyThread extends Thread {
    public void run() {
        System.out.println(this.getName()+" 该线程正在执行");
    }
    
}


public class ThreadTest {

    //两个线程在运行一个是主方法的线程,另一个是mt线程
    public static void main(String[] args) {
        System.out.println("主线程1");
        MyThread mt = new MyThread();
        mt.start(); //通过start方法启动线程,而不是run方法,要注意下。 线程只能调用一次start方法
        System.out.println("主线程2");
    }

}
package com.alan.thread1;

class MyThread extends Thread {

    // 带参构造,直接通过父类带参构造函数赋值
    public MyThread(String name) {
        super(name);
    }

    // 重写run方法
    public void run() {
        for (int i = 0; i < 10; i++) {
            //可以直接使用getName方法,因为这里继承了Thread,在其中有此方法
            System.out.println(getName()+"正在运行第"+i+"次");
        }
    }

}

public class ThreadTest {

    public static void main(String[] args) {
        // 创建两个线程,带参构造直接赋值名称
        MyThread mt1 = new MyThread("线程1");
        MyThread mt2 = new MyThread("线程2");
        //运行结果是随机的
        mt1.start();
        mt2.start();
        

    }

}

实现Runnable接口创建线程

package com.alan.runnable;

class PrintRunnable implements Runnable{
    int i = 1;
    @Override
    public void run() {

        while(i<=10)
        System.out.println(Thread.currentThread().getName()+" 正在运行"+(i++)+"次");
        
    }
    
}

public class Test {

    public static void main(String[] args) {
        // 通过实现Runnable接口创建线程
        //1、将实现Runnable接口的类实例化
        PrintRunnable pr1 = new PrintRunnable();
        //2、创建一个线程,构造函数中将pr1传入
        Thread t1 = new Thread(pr1);
        //3、将线程t1 start
        t1.start();
        
        //同理再创建一个线程
        PrintRunnable pr2 = new PrintRunnable();
        Thread t2 = new Thread(pr1);        //当两个线程初始化用同一个实现runnable的对象赋值时,相当于两个线程同时完成一个任务。
        t2.start();


        

    }

}

线程的状态和生命周期

sleep方法使用

package com.alan.runnable;

class MyThread implements Runnable {

    @Override
    public void run() {
        for (int i = 1; i <=15; i++) {
            System.out.println(Thread.currentThread().getName() + "执行第" + i + "次");
            //在run方法中调用sleep,一定要处理异常
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

public class SleepDemo {

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

    }

}

join方法的使用

package com.alan.runnable;

class MyThread1 extends Thread {

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

}

public class JoinDemo {

    public static void main(String[] args) {

        MyThread1 mt = new MyThread1();
        mt.start();
        try {
            //可以带参,也可以不带参,单位毫秒
            mt.join(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 1; i <= 20; i++) {
            System.out.println("主线程正在执行第" + i + "次");
        }
        System.out.println("主程序运行结束!");
    }

}

线程的优先级

package com.alan.runnable;

class MyThread2 extends Thread {
    
    //带参构造
    public MyThread2(String name) {
        super(name);
    }
    
    public void run() {
        for (int i = 1; i <= 20; i++)
            System.out.println(getName() + "正在执行第" + i + "次");
    }

}

public class PriorityDemo {

    public static void main(String[] args) {
        
        int mainPriority = Thread.currentThread().getPriority();
        System.out.println("主线程的默认优先级为:"+mainPriority);
        
        MyThread2  mt1 = new MyThread2("线程1");
        MyThread2  mt2 = new MyThread2("线程2");
        mt1.setPriority(Thread.MAX_PRIORITY);
        mt2.setPriority(Thread.MIN_PRIORITY);
        mt1.start();
        mt2.start();

    }

}

线程同步


线程间通信

package com.alan.queue;

public class Queue {
    
    private int n;
    
    boolean flag = false;
    
    //消费
    public synchronized int get() {
        if(!flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("消费:"+n);
        flag = false;
        this.notifyAll();
        return n;
    }

    //生产
    public synchronized void set(int n) {
        if(flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("生产:"+n);
        this.n = n;
        flag = true;
        //添加notifyAll方法,否则会造成死锁
        this.notifyAll();
    }
    
    

}
package com.alan.queue;

public class Consumer implements Runnable {
    
    Queue queue;
    
    public Consumer(Queue queue) {
        super();
        this.queue = queue;
    }


    @Override
    public  void run() {
        while(true) {
            queue.get();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
package com.alan.queue;

public class Producer implements Runnable {
    
    Queue queue;

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

    @Override
    public  void run() {
        int i = 0;
        while(true) {
            queue.set(++i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
package com.alan.queue;

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();

    }

}
上一篇下一篇

猜你喜欢

热点阅读