设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.

来源:互联网 发布:软件测评中心 编辑:程序博客网 时间:2024/06/10 07:48
public class Test2 {public static void main(String[] args) {Factory factory = new Factory();T1 t1 = new T1(factory);T2 t2 = new T2(factory);Thread thread1 = new Thread(t1, "加线程1");Thread thread2 = new Thread(t1, "加线程2");Thread thread3 = new Thread(t2, "减线程1");Thread thread4 = new Thread(t2, "减线程2");thread1.start();thread2.start();thread3.start();thread4.start();}}class T1 implements Runnable {Factory factory = null;T1(Factory factory) {this.factory = factory;}@Overridepublic void run() {while (true) {factory.add();try {Thread.sleep((int) Math.random() * 10);} catch (InterruptedException e) {e.printStackTrace();}}}}class T2 implements Runnable {Factory factory = null;T2(Factory factory) {this.factory = factory;}@Overridepublic void run() {while (true) {factory.min();try {Thread.sleep((int) Math.random() * 10);} catch (InterruptedException e) {e.printStackTrace();}}}}class Factory {int j;Factory() {j = 0;}synchronized void add() {j++;System.out.println(Thread.currentThread().getName()+":"+j);}synchronized void min() {j--;System.out.println(Thread.currentThread().getName()+":"+j);}

}

  1. package com.bookshop.util;  
  2.   
  3. public class Test {  
  4.     private int j = 100;  
  5.   
  6.     public static void main(String args[]) {  
  7.         Test tt = new Test();  
  8.         Inc inc = tt.new Inc();  
  9.         Dec dec = tt.new Dec();  
  10.   
  11.         for (int i = 0; i < 1000; i++) {// 外层用来跑1000次  
  12.             for (int j = 0; j < 2; j++) {// 内层用来生成四个线程  
  13.                 Thread t = new Thread(inc);  
  14.                 t.start();  
  15.                 Thread t1 = new Thread(dec);  
  16.                 t1.start();  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     private synchronized void inc() {  
  22.         j++;  
  23.         System.out.println(Thread.currentThread().getName() + "-inc:" + j);  
  24.     }  
  25.   
  26.     private synchronized void dec() {  
  27.         j--;  
  28.         System.out.println(Thread.currentThread().getName() + "-dec:" + j);  
  29.     }  
  30.   
  31.     class Inc implements Runnable {  
  32.         public void run() {  
  33.             inc();  
  34.         }  
  35.     }  
  36.   
  37.     class Dec implements Runnable {  
  38.         public void run() {  
  39.             dec();  
  40.         }  
  41.     }  
  42. }  

三个类: 加1: 

Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class AddNum extends Thread {  
  4.     private AtomicInteger i;  
  5.   
  6.     public AddNum(AtomicInteger i) {  
  7.         this.i = i;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         synchronized (i) {  
  13.             i.getAndIncrement();  
  14.             System.out.println(i);  
  15.         }  
  16.     }  
  17. }  


减1: 

Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class SubstractNum extends Thread {  
  4.     private AtomicInteger i;  
  5.   
  6.     public SubstractNum(AtomicInteger i) {  
  7.         this.i = i;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         synchronized (i) {  
  13.             i.decrementAndGet();  
  14.             System.out.println(i);  
  15.         }  
  16.     }  
  17. }  


测试类: 
Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class TestThread {  
  4.   
  5.     public static void main(String[] args) {  
  6.         AtomicInteger i = new AtomicInteger(30);  
  7.   
  8.         for (int j = 0; j < 1000; j++) {  
  9.             new AddNum(i).start();  
  10.             new SubstractNum(i).start();  
  11.         }  
  12.     }  
  13. }  

三个类: 加1: 

Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class AddNum extends Thread {  
  4.     private AtomicInteger i;  
  5.   
  6.     public AddNum(AtomicInteger i) {  
  7.         this.i = i;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         synchronized (i) {  
  13.             i.getAndIncrement();  
  14.             System.out.println(i);  
  15.         }  
  16.     }  
  17. }  


减1: 

Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class SubstractNum extends Thread {  
  4.     private AtomicInteger i;  
  5.   
  6.     public SubstractNum(AtomicInteger i) {  
  7.         this.i = i;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         synchronized (i) {  
  13.             i.decrementAndGet();  
  14.             System.out.println(i);  
  15.         }  
  16.     }  
  17. }  


测试类: 
Java代码  收藏代码
  1. import java.util.concurrent.atomic.AtomicInteger;  
  2.   
  3. public class TestThread {  
  4.   
  5.     public static void main(String[] args) {  
  6.         AtomicInteger i = new AtomicInteger(30);  
  7.   
  8.         for (int j = 0; j < 1000; j++) {  
  9.             new AddNum(i).start();  
  10.             new SubstractNum(i).start();  
  11.         }  
  12.     }  
  13. }  

注:这里inc方法和dec方法加synchronized关键字是因为当两个线程同时操作同一个变量时,就算是简单的j++操作时,在系统底层也是通过多条机器语句来实现,所以在执行j++过程也是要耗费时间,这时就有可能在执行j++的时候,另外一个线程H就会对j进行操作,因此另外一个线程H可能操作的可能就不是最新的值了。因此要提供线程同步

0 0
原创粉丝点击