java多线程示例

来源:互联网 发布:php活动报名系统源码 编辑:程序博客网 时间:2024/05/17 00:17

1. 多线程实现方法一

    创建任务类和线程,任务类必须实现Runnable接口,它只包含一个run方法。实现如下:

package test;
public class Test {
    public static void main(String[] args) {
        //创建任务
        PrintString print = new PrintString("jiang");
        //创建任务的线程
        Thread thread = new Thread(print);
        //调用start告诉java虚拟机线程已准备就绪
        thread.start(); 
    }
}
//定义任务类,实现Runnable接口,重载run方法
class PrintString implements Runnable{
    private String strToPrint;
    public PrintString(String str){
        this.strToPrint = str;
    }
    public void run() {
        System.out.println(this.strToPrint);
    }
}

2. 多线程实现方法二

    因为Thread类实现了Runnable接口,故可以定义一个Thread类的扩展类,并且实现run方法。它将任务和运行任务的机制混在了一起,故不推荐使用这种方法

package test;
public class Test {
    public static void main(String[] args) {
        TestThread tt = new TestThread("qin");
        tt.start();
    }
}
//直接定义Thread类的扩展类,实现run方法
class TestThread extends Thread{
    private String strToPrint;
    public TestThread(String str){
        this.strToPrint = str;
    }
    public void run() {
        System.out.println(this.strToPrint);
    }
}
3. 线程池的使用

package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
    public static void main(String[] args) {
        //创建一个最大线程数为3的线程执行器
        ExecutorService executor1 = Executors.newFixedThreadPool(3);
        //为每个等待的任务创建一个新线程,所有的任务都并发的执行
        ExecutorService executor2 = Executors.newCachedThreadPool();
        executor1.submit(new PrintString("wang"));
        executor2.submit(new PrintString("mao"));   
    }
}
//定义任务类,实现Runnable接口,重载run方法
class PrintString implements Runnable{
    private String strToPrint;
    public PrintString(String str){
        this.strToPrint = str;
    }
    public void run() {
        System.out.println(this.strToPrint);
    }
}

4. 线程同步的实现(隐式锁)

package test;
public class Test {
    private static PrintStr ps = new PrintStr();
    public static void main(String[] args) {
        //创建两个线程去同时访问PrintStr类的同步方法
        PrintString print1 = new PrintString();
        PrintString print2 = new PrintString();
        Thread thread1 = new Thread(print1);
        Thread thread2 = new Thread(print2);
        thread1.start();
        thread2.start();
    }
    //定义任务类,实现Runnable接口,重载run方法
    private static class PrintString implements Runnable{
        public void run() {
            ps.printStri();
        }
    }
    private static class PrintStr{
        //测试同步方法
        public synchronized void printStr(){
            for(int i=0; i<100;i++){
                System.out.println(i);
            }
        }
        //测试同步块
        public void printStri(){
            synchronized(this){
                for(int i=0; i<100;i++){
                    System.out.println(i);
                }
            }
        }
    }
}

5. 线程同步的实现(显式加锁)

package test;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
    private static PrintStr ps = new PrintStr();
    public static void main(String[] args) {
        //创建两个线程去同时访问PrintStr类的同步方法
        PrintString print1 = new PrintString();
        PrintString print2 = new PrintString();
        Thread thread1 = new Thread(print1);
        Thread thread2 = new Thread(print2);
        thread1.start();
        thread2.start();
    }
    //定义任务类,实现Runnable接口,重载run方法
    private static class PrintString implements Runnable{
        public void run() {
            ps.printStr();
        }
    }
    private static class PrintStr{
        //创建一个锁
        private static Lock lock = new ReentrantLock();
        //测试同步方法
        public void printStr(){
            //加锁
            lock.lock();
            try{
                for(int i=0; i<100;i++){
                    System.out.println(i);
                }
            } catch(Exception e){
                e.printStackTrace();
            }finally {
                //解锁
                lock.unlock();
            }
            
        }
    }
}

原创粉丝点击