多线程交替运行以及ExecutorService 中Callable的使用

// 多线程交替运行
public class test{
// 采用 Runnable 接口方式创建的多条线程可以共享实例属性
private int j=0;
//同步增加方法
private static Object lock = new Object();

private void inc(){

j ++;

System. out .println(Thread.currentThread().getName()+ "--inc:" + j );

}
//同步减算方法

private void dec(){

j --;

System. out .println(Thread.currentThread().getName()+ "--dec:" + j );

}

//增加线程

class Inc implements Runnable {
public void run() {
for ( int i = 0; i < 50; i++) {
synchronized (lock) {
if (j==1) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
inc();
lock.notify();
}
}
}
}
}

// 减算线程

class Dec implements Runnable{
public void run() {
for ( int i = 0; i < 50; i++) {
synchronized (lock) {
synchronized (lock) {
if (j==0) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
dec();
lock.notify();
}
}
}
}
}
}

public static void main(String[] args) {
test t = new test();
// 内部类的实例化
Inc inc = t.new Inc();
Dec dec = t.new Dec();
// 创建 2*n 个线程 此处 n=2
Thread thread = new Thread(inc);
Thread thread1 = new Thread(dec);
thread.start();
thread1.start();

}

}

方法二:
private int j=0;
private synchronized void inc(){
try {
if (1==j) {
wait();
}
notify();
j ++;
System. out .println(Thread.currentThread().getName()+ "--inc:" + j );
} catch (InterruptedException e) {
e.printStackTrace();
}

}

//同步减算方法
private synchronized void dec(){
try {
if (0==j) {
wait();
}
notify();
j --;
System. out .println(Thread.currentThread().getName()+ "--dec:" + j );
} catch (InterruptedException e) {
e.printStackTrace();
}
}


//增加线程
class Inc implements Runnable {
public void run() {
for ( int i = 0; i < 100; i++) {
inc();
}
}
}

// 减算线程
class Dec implements Runnable{
public void run() {
for ( int i = 0; i < 100; i++) {
dec();
}
}
}


// 多线程Callable返回结果集
public void testSingleThreadExecutor() throws Exception {

ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
String url = "http://192.168.1.189:9010/datacenter/TblMdMap/querySireefsys";
String s==httpAPIService.doGet(url);
return s;
}
});
System.out.println("future.get() = " + future.get());
}

// 多线程实现异步运行
@Test
public void ad(){
final CountDownLatch latch = new CountDownLatch(2);
new Thread(){
public void run() {
try {
/* String url = "http://192.168.1.189:9010/datacenter/TblMdMap/queryFileUrl?pageNum=1&pageSize=10";
String s=httpAPIService.sendGet(url);*/
for (int i=0;i<1000;i++){
System.out.println("任务__________:"+i);
}
Thread.sleep(0);
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
};
}.start();

new Thread(){
public void run() {
try {
/*String url = "http://192.168.1.189:9010/datacenter/TblMdMap/queryFileUrl?pageNum=1&pageSize=10";
String s=httpAPIService.sendGet(url);*/
for (int i=0;i<1000;i++){
System.out.println("任务3:"+i);
}
Thread.sleep(0);
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
};
}.start();

try {
latch.await();
System.out.println("2个子线程已经执行完毕____________");
System.out.println("继续执行主线程");
} catch (InterruptedException e) {
e.printStackTrace();
}
}

转载于:https://www.cnblogs.com/yooy/p/10062319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值