Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54077
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2018-08-26 01:30
文章分类

全部博文(51)

文章存档

2020年(2)

2018年(49)

我的朋友

分类: Java

2018-08-29 05:35:25


      同步出现的原因是当线程中使用共享资源时候,为了资源的独占性。这样可以避免获得结果是不正确的。

      如果,不是使用共享资源,不建议使用同步!因为这会使多线程变成单线程!而且处理不当,会引起死锁!


比如我们所写线程程序要的结果是:

Count value is: 1
Count value is: 2
Count value is: 3
Count value is: 4


     1. 方法同步


没有方法同步的代码:


  1. public class SynchDemo2 implements Runnable {
  2.     StringBuffer buffer;
  3.     int counter;
  4.     
  5.     public SynchDemo2() {
  6.         // TODO Auto-generated constructor stub
  7.         buffer = new StringBuffer();
  8.         counter = 1;
  9.     }
  10.     
  11.     public void addMessage(String message) {
  12.         int tempVariable = counter++;
  13.         buffer.append(message + tempVariable + "/r/n");
  14.     }
  15.     
  16.     public void run() {
  17.         // TODO Auto-generated method stub
  18.         String message = "Count value is: ";
  19.         
  20.         try {
  21.             Thread.sleep(500);
  22.         } catch (InterruptedException iex) {
  23.             // TODO: handle exception
  24.         }
  25.         addMessage(message);
  26.     }
  27.     /** * @param args */
  28.     public static void main(String[] args) throws InterruptedException {
  29.         // TODO Auto-generated method stub
  30.         SynchDemo2 sd = new SynchDemo2();
  31.         Thread t1 = new Thread(sd);
  32.         Thread t2 = new Thread(sd);
  33.         Thread t3 = new Thread(sd);
  34.         Thread t4 = new Thread(sd);
  35.         t1.start();
  36.         t2.start();
  37.         t3.start();
  38.         t4.start();
  39.         t1.join();
  40.         t2.join();
  41.         t3.join();
  42.         t4.join();
  43.         System.out.println(sd.buffer);
  44.     }
  45.     
  46. }



但是,运行这个程序时,每次排列都是不同的!怎样才能得到我们想要的结果呢?

只需要同步方法addMessage(),即在前面加上“synchronized”关键字!


有方法同步的代码:



  1. public class SynchDemo2 implements Runnable {
  2.     StringBuffer buffer;
  3.     int counter;
  4.     
  5.     public SynchDemo2() {
  6.         // TODO Auto-generated constructor stub
  7.         buffer = new StringBuffer(); counter = 1;
  8.     }
  9.     
  10.     public synchronized void addMessage(String message) {
  11.         int tempVariable = counter++;
  12.         buffer.append(message + tempVariable + "/r/n");
  13.     }
  14.     
  15.     public void run() {
  16.         // TODO Auto-generated method stub
  17.         String message = "Count value is: ";
  18.         try {
  19.             Thread.sleep(500);
  20.         } catch (InterruptedException iex) {
  21.             // TODO: handle exception
  22.         }
  23.         
  24.         addMessage(message);
  25.     }
  26.     
  27.     /** * @param args */
  28.     public static void main(String[] args) throws InterruptedException {
  29.         // TODO Auto-generated method stub
  30.         SynchDemo2 sd = new SynchDemo2();
  31.         Thread t1 = new Thread(sd);
  32.         Thread t2 = new Thread(sd);
  33.         Thread t3 = new Thread(sd);
  34.         Thread t4 = new Thread(sd);
  35.         t1.start();
  36.         t2.start();
  37.         t3.start();
  38.         t4.start();
  39.         t1.join();
  40.         t2.join();
  41.         t3.join();
  42.         t4.join();
  43.         System.out.println(sd.buffer);
  44.     }
  45.     
  46. }


     2. 块同步


没有块同步的代码:



点击(此处)折叠或打开

  1. public class SynchDemo implements Runnable {
  2.     StringBuffer buffer;
  3.     int counter;
  4.     public SynchDemo() {
  5.         // TODO Auto-generated constructor stub
  6.         buffer = new StringBuffer();
  7.         counter = 1;
  8.     }
  9.     
  10.     public void run() {
  11.         // TODO Auto-generated method stub
  12.         int tempVariable = counter++;
  13.         String message = "Count value is: " + tempVariable + "/r/n";
  14.         
  15.         try {
  16.             Thread.sleep(500);
  17.         } catch (InterruptedException iex) {
  18.             // TODO: handle exception
  19.         }
  20.         
  21.         buffer.append(message);
  22.     }
  23.     
  24.     /**
  25.      * @param args
  26.      */
  27.     public static void main(String[] args) throws InterruptedException {
  28.         // TODO Auto-generated method stub
  29.         SynchDemo sd = new SynchDemo();
  30.         Thread t1 = new Thread(sd);
  31.         Thread t2 = new Thread(sd);
  32.         Thread t3 = new Thread(sd);
  33.         Thread t4 = new Thread(sd);
  34.         t1.start();
  35.         t2.start();
  36.         t3.start();
  37.         t4.start();
  38.         t1.join();
  39.         t2.join();
  40.         t3.join();
  41.         t4.join();
  42.         System.out.println(sd.buffer);
  43.     }
  44.     
  45. }



但是,运行这个程序时,每次排列都是不同的!怎样才能得到我们想要的结果呢?

只需要把我们要同步的块前加“synchronized”关键字!


代码如下:


点击(此处)折叠或打开

  1. public class SynchDemo implements Runnable {
  2.     StringBuffer buffer;
  3.     int counter;
  4.     public SynchDemo() {
  5.         // TODO Auto-generated constructor stub
  6.         buffer = new StringBuffer();
  7.         counter = 1;
  8.     }
  9.     
  10.     public void run() {
  11.         // TODO Auto-generated method stub
  12.         synchronized (buffer) {
  13.             int tempVariable = counter++;
  14.             String message = "Count value is: " + tempVariable + "/r/n";
  15.             
  16.             try {
  17.                 Thread.sleep(500);
  18.             } catch (InterruptedException iex) {
  19.                 // TODO: handle exception
  20.             }
  21.             
  22.             buffer.append(message);
  23.         }
  24.         
  25.     }
  26.     
  27.     /**
  28.      * @param args
  29.      */
  30.     public static void main(String[] args) throws InterruptedException {
  31.         // TODO Auto-generated method stub
  32.         SynchDemo sd = new SynchDemo();
  33.         Thread t1 = new Thread(sd);
  34.         Thread t2 = new Thread(sd);
  35.         Thread t3 = new Thread(sd);
  36.         Thread t4 = new Thread(sd);
  37.         t1.start();
  38.         t2.start();
  39.         t3.start();
  40.         t4.start();
  41.         t1.join();
  42.         t2.join();
  43.         t3.join();
  44.         t4.join();
  45.         System.out.println(sd.buffer);
  46.     }
  47.     
  48. }



阅读(381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~