您的位置:首页 > 编程语言 > Java开发

Java多线程与并发库高级应用 学习笔记 1-9课

2015-09-16 21:04 351 查看
来源XXX,免得打广告嫌疑。
http://www.cnblogs.com/whgw/archive/2011/10/03/2198506.html
今天看了文章才发现创建线程最佳方式为实现Runnable接口,之前的习惯要改鲁。
http://blog.csdn.net/imzoer/article/details/8500670
Java中Timer的用法

package timer;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {
static Timer timer = new Timer();

public static void main(String[] args) {

// task task to be scheduled.
// delay delay in milliseconds before task is to be executed.
// period time in milliseconds between successive task executions.
timer.schedule(new MyTimerTask(), 2000);
}

}
/*
* 执行间隔2秒与4秒交替进行
*/
class MyTimerTask extends TimerTask {
static boolean change = true;
Calendar calendar = Calendar.getInstance();

@Override
public void run() {
change = !change;
System.out.println(calendar.getTime());
new Timer().schedule(new MyTimerTask(), change ? 2000 : 4000);
}

}


线程安全的在于多线程对同一资源对象的读、写、事务完成阶段。

/article/4672864.html

java synchronized关键字详解

弄明白synchronized这个关键字在不同情况锁住的对象粒度,以及范围。

package Thread;

/**
* 子线程2次,主线程4次交替循环10次。
*
*/
public class AlternateLoop {
static Sysout sysout = new Sysout();

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
sysout.subBusiness(i);
sysout.mainBusiness(i);
}
}

}

// class MyThread implements Runnable {
// @Override
// public void run() {
// AlternateLoop.sysout.subBusiness(1);
// }
// }
//
// class MyThread2 implements Runnable {
//
// @Override
// public void run() {
// AlternateLoop.sysout.mainBusiness(i);
// }
// }

/*
* 同一资源管理业务逻辑
*/
class Sysout {
private boolean isSub = true;

public synchronized void mainBusiness(int i) {
while (isSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 4; j++) {
System.out.println("mainBusiness" +j+"loop of"+ i);
}
isSub = true;
this.notify();
}

public synchronized void subBusiness(int i) {
while (!isSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 2; j++) {
System.out.println("subBusiness" +j+"loop of"+ i);
}

isSub = false;
this.notify();
}
}


Java并发编程:深入剖析ThreadLocal

每个线程存在对应的线程副本对象。

package Thread;

import java.util.Random;

public class ThreadLocalTest {

public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();

public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
int data=new Random().nextInt();
System.out.println(Thread.currentThread().getName()+"has put data :"+data);
threadLocal.set(data);
new A().get();
new B().get();
}
}).start();
}

}
static class A {
public void get() {
int data=threadLocal.get();
System.out.println("A from Thread" + Thread.currentThread().getName() + ""
+ data);
}
}

static class B {
public void get() {
int data=threadLocal.get();
System.out.println("B from Thread" + Thread.currentThread().getName() + ""
+ data);
}
}
}


上面的例子需要改造一下 这种内部静态类用起来不科学啊。

java线程:Atomic(原子的) jdk 1.5的新特性

Java并发编程:线程池的使用

package Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

public static void main(String[] args) {
ExecutorService threadPool =Executors.newCachedThreadPool();
//        ExecutorService threadPool =Executors.newFixedThreadPool(10);
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();

for (int i = 0; i < 10; i++) {
final int task = 1;
threadPool.execute(new Runnable() {

@Override
public void run() {
for (int j = 0; j < 10; j++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "is looping of " + j + "for task of" + task);
}
}
});
}

Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("bombing!");
}
}, 6, 2, TimeUnit.SECONDS);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: