java并发包使用(二)

/myConcurrent/src/reEntryLock.java

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;



public class reEntryLock implements Runnable {

public static ReentrantLock lock1 = new ReentrantLock();

public static ReentrantLock lock2 = new ReentrantLock();

public static Condition con = lock1.newCondition();

public int lockFlag = 0;

public reEntryLock(int flag) {

// TODO Auto-generated constructor stub

this.lockFlag = flag;

}

public void run(){

// TODO Auto-generated method stub

Thread.currentThread().setName("codestorm04");

if(lockFlag==0)

{

try {

lock1.lockInterruptibly();

System.out.println("getting lock1 in thead x---");

Thread.sleep(3000);

// con.await();

System.out.println("getting lock2 in thead x---");

lock2.lockInterruptibly();

// lock2.tryLock(5, TimeUnit.SECONDS);

System.out.println("stopping thread x---");

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else{

try {

lock2.lockInterruptibly();

System.out.println("getting lock2 in thead y---");

Thread.sleep(3000);

System.out.println("getting lock1 in thead y---");

lock1.lockInterruptibly();

// lock1.tryLock(5, TimeUnit.SECONDS);

// con.signal();

System.out.println("stopping thread y---");

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(lock1.isHeldByCurrentThread())

lock1.unlock();

if(lock2.isHeldByCurrentThread())

lock2.unlock();

}

public static void main(String[] args) throws InterruptedException {

Thread th1 = new Thread(new reEntryLock(0));

Thread th2 = new Thread(new reEntryLock(1));

th1.start();

th2.start();

// th1.join();

// th2.join();

Thread.sleep(4000);

System.out.println("getting lock1 in thead main------");

// th1.interrupt();

}

}





/myConcurrent/src/semaphore.java

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;



public class semaphore implements Runnable{

public final Semaphore s = new Semaphore(5,true);

public void run() {

// TODO Auto-generated method stub

try {

s.acquire();

Thread.sleep(2000);

System.out.println(Thread.currentThread()+" done!");

s.release();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

ExecutorService es = Executors.newFixedThreadPool(5); 

semaphore sem = new semaphore();

for(int i=0;i<20;i++)

{

es.submit(sem);

System.err.println("------------------");

}

// es.shutdownNow();

es.shutdown();  // or else the thread pool will be longtime thread that threads will not terminated

}

}





/myConcurrent/src/synchronize.java



public class synchronize implements Runnable {

public static Integer count=0;

public static Object mutex = new Object();

public void run() {

// TODO Auto-generated method stub

for (int i=0;i<1000;i++)

{

synchronized (mutex) {  //synchronized (this) {

count++;

}

// synchronized (count) {  //lock on count is not working for count is a changing object over time

// count++;

// }

}

}



public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

synchronize sy = new synchronize();

synchronize sy2 = new synchronize();

Thread t1 = new Thread(sy);

Thread t2 = new Thread(sy2);

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println(count);

}



}





/myConcurrent/src/AtomicIntegerCompareTest.java

import java.util.concurrent.atomic.AtomicInteger;  

public class AtomicIntegerCompareTest {  

    private int value;  

      

    public AtomicIntegerCompareTest(int value){  

        this.value = value;  

    }  

      

    public synchronized int increase(){  

        return value++;  

    }  

      

    public static void main(String args[]){  

        long start = System.currentTimeMillis();  

          

        AtomicIntegerCompareTest test = new AtomicIntegerCompareTest(0);  

        for( int i=0;i< 1000000;i++){  

            test.increase();  

        }  

        long end = System.currentTimeMillis();  

        System.out.println("time elapse:"+(end -start));  

          

        long start1 = System.currentTimeMillis();  

          

        AtomicInteger atomic = new AtomicInteger(0);  

          

        for( int i=0;i< 1000000;i++){  

            atomic.incrementAndGet();  

        }  

        long end1 = System.currentTimeMillis();  

        System.out.println("time elapse:"+(end1 -start1) );  

          

          

    }  

}  

猜你喜欢

转载自blog.csdn.net/weixin_43996899/article/details/91986263