可见性的测试——T2310

package 对象及变量的并发访问2;

import org.omg.PortableInterceptor.SUCCESSFUL;
import 多线程技能1.Run2;

import java.nio.channels.FileChannel;
import java.util.WeakHashMap;

/**
 *   可见性的测试
 *   关键字volatile具有可见性,可见性是指A线程更改变量后,B线程马上能够看到更改后的
 *数据,提高程序灵敏度。
 *
 *   线程停止不了的主要原因是main线程值在处理while()循环,导致程序不能继续执行后面的代码。
 */
 class PrintStringT2310{
     private boolean isContinuePrint =true;

     public boolean isContinuePrint(){
         return isContinuePrint;
     }

     public void setContinuePrint(boolean isContinuePrint){
         this.isContinuePrint =isContinuePrint;
     }

     public void PrintStringMethod(){
         try {
             while (isContinuePrint==true){
                 System.out.println("run printStringMethod threadName="+Thread.currentThread().getName());
                 Thread.sleep(1000);
             }
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
}

class RunT2310{
     public RunT2310(){
         PrintStringT2310 printStringT2310=new PrintStringT2310();
         printStringT2310.PrintStringMethod();
         System.out.println("停止"+Thread.currentThread().getName());
         printStringT2310.setContinuePrint(false);
     }
}

/**
 *   2.使用多线程解决死循环
 */

class PrintStrig2T2310 implements Runnable{
    private boolean isContinuePrint=true;

    public boolean isContinuePrint(){
        return isContinuePrint;
    }

    public void setContinuePrint(boolean continuePrint) {
        isContinuePrint = continuePrint;
    }

    public void printStringMethod(){
        try {
            while (isContinuePrint==true){
                System.out.println("run printStringMethod Name="+Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        printStringMethod();
    }
}

class Run2T2310{
    public Run2T2310(){
        PrintStrig2T2310 printStrig2T2310=new PrintStrig2T2310();
        new Thread(printStrig2T2310).start();

        System.out.println("停止"+Thread.currentThread().getName());
        printStrig2T2310.setContinuePrint(false);
    }
}

/**
 *   3.使用多线程可能出现的死循环
 */

class RunThread extends Thread{
    private boolean isRunning =true;

    public boolean isRunning(){
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public void run() {
        System.out.println("进入run");
        while (isRunning==true){

        }
        System.out.println("停止了");
    }
}

class Run3T2310{
    public Run3T2310(){
        try {
            RunThread runThread=new RunThread();
            runThread.start();
            Thread.sleep(1000);
            System.out.println("赋值false");
            runThread.setRunning(false);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 *   4.使用volatile关键字解决多线程出现的死循环
 *   造成死循环的原因:在启动RunThread.java线程时,变量“print boolean isRunning =true;"存在
 *与公共堆栈及线程的私有堆栈中,线程运行后一直在线程的私有堆栈中取得的isRunning的值是true,而代码“
 * thread.setRunning(false);"虽然被执行,更新的却是公共堆栈中的isRunning变量,改为了false,操作
 * 的是两块内存地址中的数据,所以线程一直处于死循环状态
 *
 */

class RunThread4 extends Thread{
    volatile private boolean isRunning =true;

    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public void run() {
        System.out.println("进入");
        while (isRunning==true){

        }
        System.out.println("停止了");
    }
}

class Run4T2310{
    public Run4T2310(){
        try {
            RunThread4 runThread4=new RunThread4();
            runThread4.start();
            Thread.sleep(1000);
            System.out.println("赋值false");
            runThread4.setRunning(false);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 *  5。synchronized代码块具有增加可见性的作用
 *  关键字synchronized可以使用多线程访问同一个资源具有同步性,而且具有使用线程
 *工作内存中的私有变量与共有内存中的变量同步的特性,即可见性。
 */

class Service5T2310{
    private boolean isContinueRun=true;

    public void runMethod(){
        while (isContinueRun==true){

        }
        System.out.println("停止");
    }

    public void stopMethod(){
        isContinueRun=false;
    }
}

class MyThreadAT2310 extends Thread{
    private Service5T2310 service5T2310;

    public MyThreadAT2310(Service5T2310 service5T2310){
        super();
        this.service5T2310=service5T2310;
    }

    @Override
    public void run() {
        service5T2310.runMethod();
    }
}

class MyThreadBT2310 extends Thread{
    private  Service5T2310 service5T2310;

    public MyThreadBT2310(Service5T2310 service5T2310){
        super();
        this.service5T2310=service5T2310;
    }

    @Override
    public void run() {
        service5T2310.stopMethod();
    }
}

class Run5T2310{
    public Run5T2310(){
        try {
            Service5T2310 service5T2310=new Service5T2310();

            MyThreadAT2310 at2310=new MyThreadAT2310(service5T2310);
            at2310.start();

            Thread.sleep(1000);

            MyThreadBT2310 bt2310=new MyThreadBT2310(service5T2310);
            bt2310.start();

            System.out.println("发出停止");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Service52T2310{
    private boolean isContinueRun=true;

    public void runMethod(){
        String anyString =new String();
        while (isContinueRun==true){
            synchronized (anyString){

            }
        }
        System.out.println("停了");
    }

    public void stopMethod(){
        isContinueRun= false;
    }
}

class MyThreadA5T2310 extends Thread{
    private Service52T2310 service52T2310;

    public MyThreadA5T2310 (Service52T2310 service52T2310){
        super();
        this.service52T2310=service52T2310;
    }

    @Override
    public void run() {
        service52T2310.runMethod();
    }
}
class MyThreadB5T2310 extends Thread{
    private  Service52T2310 service52T2310;

    public MyThreadB5T2310(Service52T2310 service52T2310){
        super();
        this.service52T2310=service52T2310;
    }

    @Override
    public void run() {
        service52T2310.stopMethod();
    }
}
 class Run52T2310{
    public Run52T2310() throws InterruptedException {
        Service52T2310 service52T2310=new Service52T2310();

        MyThreadA5T2310 a5T2310=new MyThreadA5T2310(service52T2310);
        a5T2310.start();

        Thread.sleep(1000);

        MyThreadB5T2310 b5T2310=new MyThreadB5T2310(service52T2310);
        b5T2310.start();

        System.out.println("发出停止");
    }
 }
public class T2310 {
    public static void main(String[] args) throws InterruptedException {
        RunT2310 runT2310=new RunT2310();
        //Run2T2310 run2T2310=new Run2T2310();
        //Run3T2310 run3T2310=new Run3T2310();
        //Run4T2310 run4T2310=new Run4T2310();
        //Run5T2310 run5T2310 =new Run5T2310();
        //Run52T2310 run52T2310=new Run52T2310();
    }
}

Run52T2310 run52T2310=new Run52T2310();
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值