java并发包使用(一)_codestorm_新浪博客

/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());

}

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值