首页 > 编程语言 >java的多线程实现方式以及对应的线程锁实现

java的多线程实现方式以及对应的线程锁实现

时间:2022-11-02 14:00:48浏览次数:40  
标签:java String void 线程 static 多线程 public name

一、多线程的实现

1.1 继承Thread类

继承:

package com.yuan.yk.ThreadLearn;

import static com.yuan.yk.ThreadLearn.func1.doSomething;

public class ThreadFunc extends Thread{

public void run(){
doSomething();
}

}

 

调用:

package com.yuan.yk.ThreadLearn;

import java.util.concurrent.TimeUnit;

public class func1 {

    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            ThreadFunc thread = new ThreadFunc();
            thread.start();
        }
    }


    public static void doSomething(){
        String name = Thread.currentThread().getName();
        try{
            System.out.println(String.format("线程: %s 开始休眠", name));
            TimeUnit.SECONDS.sleep(4);
            System.out.println(String.format("线程: %s 结束休眠", name));

        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }


}

 

1.2 继承Runable

package com.yuan.yk.RunableLearn;

import java.util.concurrent.TimeUnit;

public class RunableFunc1 implements Runnable{
    // 生成的对象唯一
    private static RunableFunc1 runableFunc1 = new RunableFunc1();

    public void run(){
        String name = Thread.currentThread().getName();
        try{
            System.out.println(String.format("线程: %s 开始休眠", name));
            TimeUnit.SECONDS.sleep(4);
            System.out.println(String.format("线程: %s 结束休眠", name));

        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            // 传入对应类的对象,需要保证该对象唯一
            Thread thread = new Thread(runableFunc1);
            thread.start();
        }
    }
}

 

二、线程锁的实现

2.1 Thread的线程锁

package com.yuan.yk.ThreadLearn;

import java.util.concurrent.TimeUnit;

public class func2 {

    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            ThreadFunc thread = new ThreadFunc();
            thread.start();
        }
    }


    // 修饰静态方法相当于锁当前类,非静态方法相当于锁当前实例对象。 静态成员不属于任何一个类对象
    public synchronized static void doSomething(){
        String name = Thread.currentThread().getName();
        try{
            System.out.println(String.format("线程: %s 开始休眠", name));
            TimeUnit.SECONDS.sleep(4);
            System.out.println(String.format("线程: %s 结束休眠", name));

        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }

}
package com.yuan.yk.ThreadLearn;

import java.util.concurrent.TimeUnit;

public class func3 {
    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            ThreadFunc thread = new ThreadFunc();
            thread.start();
        }
    }


    public static void doSomething(){
        String name = Thread.currentThread().getName();
        try{
            // 修饰代码块,(this|class) 充当唯一锁
            synchronized (func3.class){
                System.out.println(String.format("线程: %s 开始休眠", name));
                TimeUnit.SECONDS.sleep(4);
                System.out.println(String.format("线程: %s 结束休眠", name));
            }
        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }

}

 

2.2 Runable的线程锁

package com.yuan.yk.RunableLearn;

import java.util.concurrent.TimeUnit;

public class RunableFunc2 implements Runnable{

    // 生成的对象唯一
    static RunableFunc2 runableFunc2 = new RunableFunc2();

    // 自定义的锁
    Object block1 = new Object();

    public void run(){
        String name = Thread.currentThread().getName();
        try{
            // 修饰代码块, 手动指定锁对象,可以是this,也可以是自定义的锁,也可以是类的class
            synchronized (block1){
                System.out.println(String.format("线程: %s 开始休眠", name));
                TimeUnit.SECONDS.sleep(4);
                System.out.println(String.format("线程: %s 结束休眠", name));
            }


        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            // 传入对应类的对象,需要保证该对象唯一
            Thread thread = new Thread(runableFunc2);
            thread.start();
        }
    }

}
package com.yuan.yk.RunableLearn;

import java.util.concurrent.TimeUnit;

public class RunableFunc3 implements Runnable {
    // 生成的对象唯一
    static RunableFunc3 runableFunc3 = new RunableFunc3();


    public void run(){
        method();
    }

    // 修饰普通方法,锁对象默认为this ;  修饰静态方法默认的锁是当前所在的class类,无论哪个线程访问它,需要的锁都只有一把
    public synchronized void method(){
        String name = Thread.currentThread().getName();
        try{
            System.out.println(String.format("线程: %s 开始休眠", name));
            TimeUnit.SECONDS.sleep(4);
            System.out.println(String.format("线程: %s 结束休眠", name));
        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
        for (int i=0; i<10; i++){
            // 传入对应类的对象,需要保证该对象唯一
            Thread thread = new Thread(runableFunc3);
            thread.start();
        }
    }
}

 

标签:java,String,void,线程,static,多线程,public,name
From: https://www.cnblogs.com/changwenjun-666/p/16850812.html

相关文章