`
majie
  • 浏览: 74220 次
  • 来自: ...
社区版块
存档分类
最新评论

A类每0-2秒生成-100到100之间的随机整数。B类让一个计数器在A类生成负数的时候自减,并且在生成正数的时候自增。B

阅读更多
A类每0-2秒生成-100到100之间的随机整数。B类让一个计数器在A类生成负数的时候自减,并且在生成正数的时候自增。B类的计数器必须与A类同时实时更新。
把3种情况分别写出来:1,A与B在同一线程内;2,不同线程;3,不同进程
程序如下:

package practice2;

public class Practice2 {

 /**
  * @param args
  * @throws InterruptedException
  */
 /*
  * 类每0-2秒生成-100到100之间的随机整数。B类让一个计数器在A类生成负数的时候自减,
  * 并且在生成正数的时候自增。B类的计数器必须与A类同时实时更新。 把3种情况分别写出来:1,A与B在同一线程内;2,不同线程;3,不同进程
  */
 //单线程版
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  C c = new C();
  System.out.println("程序开始........");
  new Thread(c).start();

 }

}

class A {
 private static int x;

 public static void generateValue() {
  // 生成-100到100之间的随机整数
  int y;
  y = (int) (Math.random() * 2 + 1);
  if (y % 2 == 1)
   x = (int) ((Math.random() * 100 + 1));
  else
   x = -(int) ((Math.random() * 100 + 1));

 }

 public static int getX() {
  return x;
 }
}

class B {
 private static int sum;

 public static void plus(int x) {
  // 求和
  sum = sum + x;
 }

 public static int getSum() {
  return sum;
 }

}

//
class C implements Runnable {

 public void run() {
  // TODO Auto-generated method stub
  while (true) {
   int x = (int) ((Math.random() * 2 + 1) * 1000);
   try {
    Thread.sleep(x);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println("您产生的数字是:");
   A.generateValue();
   System.out.println(A.getX());
   System.out.println("和是:");
   B.plus(A.getX());
   System.out.println(B.getSum());
  }
 }

}

多线程序


package practice2;

public class Practice2duo {

 /**
  * @param args
  */
 /*
  * 类每0-2秒生成-100到100之间的随机整数。B类让一个计数器在A类生成负数的时候自减,
  * 并且在生成正数的时候自增。B类的计数器必须与A类同时实时更新。 把3种情况分别写出来:1,A与B在同一线程内;2,不同线程;3,不同进程
  */
 //多线程版
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  CDuo c = new CDuo();
  System.out.println("程序开始........");
  new Thread(c).start();
  new Thread(c).start();
 }

}

class ADuo {
 private static int x;

 public static void generateValue() {
  // 生成-100到100之间的随机整数
  int y;
  y = (int) (Math.random() * 2 + 1);
  if (y % 2 == 1)
   x = (int) ((Math.random() * 100 + 1));
  else
   x = -(int) ((Math.random() * 100 + 1));

 }

 public static int getX() {
  return x;
 }
}

class BDuo {
 private static int sum;

 public static void plus(int x) {
  // 求和
  sum = sum + x;
 }

 public static int getSum() {
  return sum;
 }

}

class CDuo implements Runnable {
 

 public void run() {
 String str = new String("");
  // TODO Auto-generated method stub
  while (true) {
   synchronized (str) {
    int x = (int) ((Math.random() * 2 + 1) * 1000);
    try {
     Thread.sleep(x);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    System.out.println("您产生的数字是:");
    A.generateValue();
    System.out.println(A.getX());
    System.out.println("和是:");
    B.plus(A.getX());
    System.out.println(B.getSum());
   }
  }
 }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics