volatile关键字

 看过一些大佬文章,这里写个笔记,方便复习,因为我现在是大一,理解可能也有些问题

 每个线程都有一个工作内存,相当于CPU高速缓存区,程序对数据的 操作是在工作内存内进行的,但什么时候刷入主内存,这个是不确定的,当只有一个线程时,这个是没有影响的,因为也就只有一个工作内存,但当有两个以上线程,并且还有共享变量,这些个线程都在各自的工作内存内操作数据,但什么时刷入缓冲区是不确定的,这也就导致了脏读问题,有的线程已经把值修改了,因为另一个线程也是在自己的工作内存中进行操作,它并不知道值已经修改过了,还是用原来的值进行操作,就出现了脏读。

使用volatile修饰后,在一个线程的工作内存中,如果这个值发生了修改,其他线程工作内存的这个值就会失效,必须重新从主内存内读取,这样就避免其他线程修改了值,却不知道的情况。

但是和synchronized还是有区别的,不能用volatile替代synchronized,使用synchronized,实现线程同步,只有拿到锁,才能使程序继续进行,同一时间,只有一个线程能对资源进行访问,访问完后写入主内存,另外一个线程拿到锁后,又从主内存中读取资源到工作内存中,在进行处理,这样就实现了线程资源的同步,当然这样一来,程序的效率也就被降低了。

下面是我的一道作业题

某科学机构为了搜索外星人,准备使用多线程技术。当线程发现传入的关键字包含alien时则输出相关信息。
定义一个MonitorTask类实现Runnable接口,包含:
属性:
private boolean flag = false; //用于停止线程的标志位,可根据需要自行决定是否使用其他关键字修饰flag
private String word;

方法:
public void stopMe(); //用来终止线程。
public void sendWord(String word) //将入参word赋值给属性word。
run() //持续监测属性word,一旦发现属性word包含alien,则输出。
输出代码为: System.out.println(Thread.currentThread().getName()+" found alien in "+word)
注: 对每个传入的word只检查一遍(检查完后将word置为null)。跳出无限循环后,使用 System.out.println(Thread.currentThread().getName() + " stop") 打印标识信息。

裁判测试程序:
import java.util.Scanner;
/*这里放置你的代码*/
public class Main {
    public static void main(String[] args) throws InterruptedException {
        MonitorTask task1 = new MonitorTask();
        Thread t1 = new Thread(task1);
        t1.start();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String word = sc.next();
            if (word.equals("stop")) {
                task1.stopMe();
            } else if (word.equals("quit")) {
                break;
            } else {
                if (t1.isAlive())
                   task1.sendWord(word);
            }
            Thread.sleep(10);
        }
        System.out.println(t1.isAlive());
        task1.stopMe();
        sc.close();
    }
}

答案:

class MonitorTask implements Runnable{
    
    private volatile boolean flag=false;//加volatile修饰
    private volatile String word;
  
    public void stopMe()
    {
        flag=true;
        
        
    }
  
    public void sendWord(String word)
    {
        
        this.word=word;
    }
    public void run()
    {
        while(!flag){
        if(word!=null)
        {
           if(  word.contains("alien"))
           {
              System.out.println(Thread.currentThread().getName()+" found alien in "+word); 
               
               
           }
            
            
               word=null;
            
        }
     
        
        
        
    }
    
    System.out.println(Thread.currentThread().getName() + " stop");
        
    }
    
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值