java并发包使用(一)

/myConcurrent/src/newThread.java



public class newThread implements Runnable{

public static void  main(String args[])

{

Thread thread = new Thread(new newThread() );

thread.start();

}



// @Override

public void run() {

// TODO Auto-generated method stub

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

{

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Hello World!");

}

}

}



/myConcurrent/src/notifyAndWait.java



import java.util.concurrent.atomic.AtomicBoolean;

import java.util.concurrent.atomic.AtomicInteger;



public class notifyAndWait implements Runnable{

public static Integer a = 1;

public static AtomicInteger b = new AtomicInteger(1);

public static void  main(String args[])

{

notifyAndWait notifyAndWait = new notifyAndWait(); 

Thread thread1 = new Thread(notifyAndWait);

Thread thread2 = new Thread(notifyAndWait);

thread1.start();

thread2.start();

}



// @Override

public void run() {

// TODO Auto-generated method stub

// try {

// Thread.sleep(4000);

// } catch (InterruptedException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

synchronized (a) {

if(a==0)

{

// a = 0;

//对于integer类型,如果改变了值则a不再是原来的a,所以监控的monitor出错,可使用atomicInteger

System.out.println(System.currentTimeMillis()+Thread.currentThread().toString()+"locking a");

try {

a.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else{

a = 1;

System.out.println(System.currentTimeMillis()+Thread.currentThread().toString()+"unlocking a");

a.notify();

}

}

}

}





/myConcurrent/src/notifyAndWait2.java

public class notifyAndWait2 {

public static Integer object = new Integer(1);

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

{

notifyAndWait2 notifyAndWait = new notifyAndWait2();

th1 t1 = notifyAndWait.new th1();

th2 t2 = notifyAndWait.new th2();

t1.start();

t2.start();

System.out.println("main thread start");

Thread.sleep(1000);

}

class th1 extends Thread{

@Override

public void run() {

// TODO Auto-generated method stub

synchronized (object) {

System.out.println(System.currentTimeMillis()+" "+Thread.currentThread().toString()+" locking a");

try {

System.out.println("th1 thread wait");

object.wait();

// Thread.sleep(1000);

System.out.println("th1 thread restart");

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

class th2 extends Thread{

@Override

public void run() {

// TODO Auto-generated method stub

synchronized (object) {

System.out.println(System.currentTimeMillis()+" "+Thread.currentThread().toString()+" unlocking a");

object.notify();

System.out.println("th2 thread notify");

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}





/myConcurrent/src/yieldJoin.java



public class yieldJoin implements Runnable{

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

{

Thread thread1 = new Thread(new yieldJoin());

Thread thread2 = new Thread(new yieldJoin());

Thread thread3 = new Thread(new yieldJoin());

Thread thread4 = new Thread(new yieldJoin());

thread1.setPriority(9);

thread2.setPriority(1);

thread3.setPriority(1);

thread4.setPriority(1);

thread1.start();

thread2.start();

thread3.start();

thread4.start();

thread1.join();

thread2.join();

thread3.join();

thread4.join();

Thread.sleep(1000);

System.out.println("main thread stopping");

}

public void run() {

// TODO Auto-generated method stub

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

{

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("running "+Thread.currentThread());

}

}

}

猜你喜欢

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