问题:线程安全方面

package com.cyj.thread.manage;


/**
 * 同步方法和同步块(需要引用内容)
 * 线性安全速度慢,所以将位置锁定太大速度慢,锁定太小位置不准确就达不到效果
 * 文件锁将资源锁住,其余的线程不会分配等待
 * 
 * 这段代码有问题
 * 1 应用了线程安全的同步控制,但只能控制数量上的安全,其操作不安全
 * 2 不是从1累加到50,有重复和缺失
 * @author Chyjrily
 *
 */
public class NetworkDalay2 implements Runnable{
	
	public static void main(String[] args) {
		//真实角色
		Cuiyongyuan cyy = new Cuiyongyuan();
		
		//代理角色
		Thread c1 = new Thread(cyy,"一号支持者");
		Thread c2 = new Thread(cyy,"二号支持者");
		Thread c3 = new Thread(cyy,"三号支持者");
		
		//启动线程
		c1.start();
		c2.start();
		c3.start();
	}
	
	 private int num = 50;
	 private boolean flag = true;
	 
	 public void run(){
		 while(flag) {
			 text4();
		 }
	 }

	//线程不安全
	public void text1() {
		if(num>=50) {
			flag = false;
			return;
		}
		
		try {
			Thread.sleep(500); //加了延时之后,数据可能不准确
		} catch (InterruptedException e) {  //不能对外声明异常
			e.printStackTrace();
			System.out.println("延时失败 ");
		}
		
		num += 1;
		if(num%2 == 0) {
		    System.out.println(Thread.currentThread().getName() + "评论支持了崔永元一下"+"共评论"+num+"次");
		}else {
			System.out.println(Thread.currentThread().getName() + "评论损了冯裤子一下"+"共评论"+num+"次" );
		}
	}
	
	//线程安全,同步方法
	 public synchronized void text2() {
		 
		 if(num>=50) {
				flag = false;
			}
			
			try {
				Thread.sleep(1000); //加了延时之后,数据可能不准确
			} catch (InterruptedException e) {  //不能对外声明异常
				e.printStackTrace();
				System.out.println("延时失败 ");
			}
			
			num = num++;
			if(num%2 == 0) {
			    System.out.println(Thread.currentThread().getName() + "评论支持了崔永元一下"+"共评论"+num+"次");
			}else {
				System.out.println(Thread.currentThread().getName() + "评论损了冯裤子一下"+"共评论"+num+"次" );
			}
		 }
	 
         public  void text3() {
		 
         synchronized(this) {  //this代表这个资源锁,这个方法如果要使用,需要添加引用类型
        	 if(num>=50) {
 				flag = false;
 			}
 			
 			try {
 				Thread.sleep(1000); //加了延时之后,数据可能不准确
 			} catch (InterruptedException e) {  //不能对外声明异常
 				e.printStackTrace();
 				System.out.println("延时失败 ");
 			}
 			
 			num += 1;
 			if(num%2 == 0) {
 			    System.out.println(Thread.currentThread().getName() + "评论支持了崔永元一下"+"共评论"+num+"次");
 			}else {
 				System.out.println(Thread.currentThread().getName() + "评论损了冯裤子一下"+"共评论"+num+"次" );
 			}
 		  }
         }
		
         public  void text4() {
    		 
               //this代表这个资源锁,这个方法如果要使用,需要添加引用类型
               if(num>=50) {
     				flag = false;
               }
     			
               
               synchronized(this) {
     			try {
     				Thread.sleep(1000); //加了延时之后,数据可能不准确
     			} catch (InterruptedException e) {  //不能对外声明异常
     				e.printStackTrace();
     				System.out.println("延时失败 ");
     			}
     			
     			num += 1;
     			if(num%2 == 0) {
     			    System.out.println(Thread.currentThread().getName() + "评论支持了崔永元一下"+"共评论"+num+"次");
     			}else {
     				System.out.println(Thread.currentThread().getName() + "评论损了冯裤子一下"+"共评论"+num+"次" );
     			}
               
     		  }
           }    

}

猜你喜欢

转载自blog.csdn.net/qq_42036616/article/details/81070515