Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782189
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-01-26 08:58:50

看了点JAVA的多线程,有点意思。访问线程共享变量,加上synchronized 就可以了。


点击(此处)折叠或打开

  1. public class ThreadTracker {
  2.     private int threadsRunning=0;

  3.     public synchronized void setThreadRunning(){
  4.         threadsRunning++;
  5.         System.out.println(threadsRunning+" threads running");

  6.     }

  7.     public void setTheadFinished(){
  8.         threadsRunning--;
  9.         System.out.println(threadsRunning+" threads running");
  10.     }
  11. }

点击(此处)折叠或打开

  1. public class NumberProcessor implements Runnable{
  2.     private long numToProcess;
  3.     private ThreadTracker tracker;


  4.     public NumberProcessor(long numToProcess, ThreadTracker tracker){
  5.         this.numToProcess=numToProcess;
  6.         this.tracker=tracker;
  7.     }

  8.     public boolean isNumberPrime(long number){
  9.         long end=number/2;
  10.         for(long i=2;i<=end;i++){
  11.             if(number % i==0){
  12.                 return false;
  13.             }
  14.         }
  15.         return true;
  16.     }

  17.     @Override
  18.     public void run(){
  19.         try{
  20.             Thread.sleep((long)Math.log(numToProcess));
  21.         }catch(InterruptedException e){}
  22.         tracker.setThreadRunning();
  23.         System.out.println("Beginning processing "+numToProcess);
  24.         boolean result=isNumberPrime(numToProcess);
  25.         if(result==true){
  26.             System.out.println(numToProcess+" is a prime number");
  27.         }else{
  28.             System.out.println(numToProcess+" is not a prime number");
  29.         }
  30.         tracker.setTheadFinished();
  31.     }


  32.     public static void main(String[] args){
  33.         ThreadTracker tracker=new ThreadTracker();
  34.         //System.out.println(Runtime.getRuntime().availableProcessors());
  35.         for(int i=0;i<args.length;i++){
  36.             long number=Long.valueOf(args[i]);
  37.             NumberProcessor numberProcessor=new NumberProcessor(number,tracker);
  38.             Thread t=new Thread(numberProcessor);
  39.             t.start();
  40.         }
  41.     }
  42. }

CONDITION 和wait 

点击(此处)折叠或打开

  1. public class WaitExample implements Runnable {
  2.     private int resultsObtained=0;

  3.     public static void main(String[] args){
  4.         WaitExample we=new WaitExample();
  5.         for(int i=0;i < 10 ;i++){
  6.             Thread t=new Thread(we);
  7.             t.start();
  8.         }
  9.         we.printResults();
  10.     }

  11.     public synchronized void printResults(){
  12.         while(resultsObtained < 5){
  13.             try{
  14.                 System.out.println("Waiting for enough results");
  15.                 wait();
  16.             }catch(InterruptedException e){}
  17.         }
  18.         System.out.println("We have received enough results");
  19.     }

  20.     public synchronized void addResult(){
  21.         resultsObtained++;
  22.         System.out.println("A result has been added");
  23.         notifyAll();
  24.     }

  25.     @Override
  26.     public void run(){
  27.         try{Thread.sleep((long)(Math.random()*10000));
  28.         }catch(InterruptedException e){}
  29.         addResult();
  30.     }
  31. }

光看这些,感觉还真简单,想想那本书 a little book of semaphore, 多线程哪里有这么简单.
spinlock , barrier, 都是些复杂的东西。

对照下C的pthread C 代码,也挺有意思的,pthread_mutex_unlock, pthread_mutex_lock,pthread_create,pthread_join


点击(此处)折叠或打开

  1. #include <apue.h>
  2. #include <errno.h>
  3. #include <pthread.h>

  4. int beers=200000;
  5. pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

  6. void* drink_lots(void *a)
  7. {
  8.     int i;
  9.     pthread_mutex_lock(&mutex);
  10.     for(i=0;i<10000;i++)
  11.     {
  12.         beers=beers-1;
  13.         }
  14.     pthread_mutex_unlock(&mutex);
  15.     printf("beers=%i\n",beers);
  16.     return NULL;
  17.     }


  18. void* drink_lots2(void *a)
  19. {
  20.     int i;
  21.     for(i=0;i<10000;i++)
  22.     {
  23.         pthread_mutex_lock(&mutex);
  24.         beers=beers-1;
  25.         pthread_mutex_unlock(&mutex);
  26.     }
  27.     printf("beers=%i\n",beers);
  28.     return NULL;
  29.     }
  30. int main(void)
  31. {
  32.     pthread_t threads[20];
  33.     int t;
  34.     printf("%i bottles of beers on the wall\n%i bottles of beer\n",beers,beers);
  35.     for(t=0;t<20;t++)
  36.     {
  37.         pthread_create(&threads[t],NULL,drink_lots2,NULL);
  38.         }
  39.     
  40.     void *result;
  41.     for(t=0;t<20;t++)
  42.     {
  43.         pthread_join(threads[t],&result);
  44.         }

  45.     printf("There are now %i bottles of beer on the wall\n",beers);
  46.     return 0;
  47.     }



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