看了点JAVA的多线程,有点意思。访问线程共享变量,加上synchronized 就可以了。
-
public class ThreadTracker {
-
private int threadsRunning=0;
-
-
public synchronized void setThreadRunning(){
-
threadsRunning++;
-
System.out.println(threadsRunning+" threads running");
-
-
}
-
-
public void setTheadFinished(){
-
threadsRunning--;
-
System.out.println(threadsRunning+" threads running");
-
}
-
}
-
public class NumberProcessor implements Runnable{
-
private long numToProcess;
-
private ThreadTracker tracker;
-
-
-
public NumberProcessor(long numToProcess, ThreadTracker tracker){
-
this.numToProcess=numToProcess;
-
this.tracker=tracker;
-
}
-
-
public boolean isNumberPrime(long number){
-
long end=number/2;
-
for(long i=2;i<=end;i++){
-
if(number % i==0){
-
return false;
-
}
-
}
-
return true;
-
}
-
-
@Override
-
public void run(){
-
try{
-
Thread.sleep((long)Math.log(numToProcess));
-
}catch(InterruptedException e){}
-
tracker.setThreadRunning();
-
System.out.println("Beginning processing "+numToProcess);
-
boolean result=isNumberPrime(numToProcess);
-
if(result==true){
-
System.out.println(numToProcess+" is a prime number");
-
}else{
-
System.out.println(numToProcess+" is not a prime number");
-
}
-
tracker.setTheadFinished();
-
}
-
-
-
public static void main(String[] args){
-
ThreadTracker tracker=new ThreadTracker();
-
//System.out.println(Runtime.getRuntime().availableProcessors());
-
for(int i=0;i<args.length;i++){
-
long number=Long.valueOf(args[i]);
-
NumberProcessor numberProcessor=new NumberProcessor(number,tracker);
-
Thread t=new Thread(numberProcessor);
-
t.start();
-
}
-
}
-
}
CONDITION 和wait
-
public class WaitExample implements Runnable {
-
private int resultsObtained=0;
-
-
public static void main(String[] args){
-
WaitExample we=new WaitExample();
-
for(int i=0;i < 10 ;i++){
-
Thread t=new Thread(we);
-
t.start();
-
}
-
we.printResults();
-
}
-
-
public synchronized void printResults(){
-
while(resultsObtained < 5){
-
try{
-
System.out.println("Waiting for enough results");
-
wait();
-
}catch(InterruptedException e){}
-
}
-
System.out.println("We have received enough results");
-
}
-
-
public synchronized void addResult(){
-
resultsObtained++;
-
System.out.println("A result has been added");
-
notifyAll();
-
}
-
-
@Override
-
public void run(){
-
try{Thread.sleep((long)(Math.random()*10000));
-
}catch(InterruptedException e){}
-
addResult();
-
}
-
}
光看这些,感觉还真简单,想想那本书 a little book of semaphore, 多线程哪里有这么简单.
spinlock , barrier, 都是些复杂的东西。
对照下C的pthread C 代码,也挺有意思的,
pthread_mutex_unlock,
pthread_mutex_lock,pthread_create,pthread_join
-
#include <apue.h>
-
#include <errno.h>
-
#include <pthread.h>
-
-
int beers=200000;
-
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
-
-
void* drink_lots(void *a)
-
{
-
int i;
-
pthread_mutex_lock(&mutex);
-
for(i=0;i<10000;i++)
-
{
-
beers=beers-1;
-
}
-
pthread_mutex_unlock(&mutex);
-
printf("beers=%i\n",beers);
-
return NULL;
-
}
-
-
-
void* drink_lots2(void *a)
-
{
-
int i;
-
for(i=0;i<10000;i++)
-
{
-
pthread_mutex_lock(&mutex);
-
beers=beers-1;
-
pthread_mutex_unlock(&mutex);
-
}
-
printf("beers=%i\n",beers);
-
return NULL;
-
}
-
int main(void)
-
{
-
pthread_t threads[20];
-
int t;
-
printf("%i bottles of beers on the wall\n%i bottles of beer\n",beers,beers);
-
for(t=0;t<20;t++)
-
{
-
pthread_create(&threads[t],NULL,drink_lots2,NULL);
-
}
-
-
void *result;
-
for(t=0;t<20;t++)
-
{
-
pthread_join(threads[t],&result);
-
}
-
-
printf("There are now %i bottles of beer on the wall\n",beers);
-
return 0;
-
}
阅读(862) | 评论(0) | 转发(0) |