前言:本系列将从零开始讲解 java 多线程相关的技术,内容参考于《java 多线程核心技术》与《java 并发编程实战》等相关资料,希望站在巨人的肩膀上, 再通过我的理解能让知识更加简单易懂。
- public void run() {
- try {
- for (int i = 0; i < 10; i++) {
- list.add();
- System.out.println("添加了" + (i + 1) + "个元素");
- Thread.sleep(1000);
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- while (true) {
- if (list.size() == 5) {
- System.out.println("==5了,线程b要退出了!");
- throw new InterruptedException();
- }
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- synchronized(lock) {
- if (MyList.size() != 5) {
- System.out.println("wait begin " + System.currentTimeMillis());
- lock.wait();
- System.out.println("wait end " + System.currentTimeMillis());
- }
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- synchronized(lock) {
- for (int i = 0; i < 10; i++) {
- MyList.add();
- if (MyList.size() == 5) {
- lock.notify();
- System.out.println("已发出通知!");
- }
- System.out.println("添加了" + (i + 1) + "个元素!");
- Thread.sleep(1000);
- }
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void setValue() {
- try {
- synchronized(lock) {
- if (!ValueObject.value.equals("")) {
- lock.wait();
- }
- String value = System.currentTimeMillis() + "_" + System.nanoTime();
- System.out.println("set" + value);
- ValueObject.value = value;
- lock.notify();
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void getValue() {
- try {
- synchronized(lock) {
- if (ValueObject.value.equals("")) {
- lock.wait();
- }
- System.out.println("get" + ValueObject.value);
- ValueObject.value = "";
- lock.notify();
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- while (true) {
- r.getValue();
- }
- }
- public void run() {
- while (true) {
- p.setValue();
- }
- }
- public void getValue() {
- try {
- synchronized(lock) {
- while (ValueObject.value.equals("")) {
- System.out.println("消费者 " + Thread.currentThread().getName() + " WAITING了☆");
- lock.wait();
- }
- System.out.println("消费者 " + Thread.currentThread().getName() + " RUNNABLE了");
- ValueObject.value = "";
- lock.notify();
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- while (true) {
- r.getValue();
- }
- }
- public void setValue() {
- try {
- synchronized(lock) {
- while (!ValueObject.value.equals("")) {
- System.out.println("生产者 " + Thread.currentThread().getName() + " WAITING了★");
- lock.wait();
- }
- System.out.println("生产者 " + Thread.currentThread().getName() + " RUNNABLE了");
- String value = System.currentTimeMillis() + "_" + System.nanoTime();
- ValueObject.value = value;
- lock.notify();
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- while (true) {
- p.setValue();
- }
- }
- public class ThreadWrite extends Thread {
- private WriteData write;
- private PipedOutputStream out;
- public ThreadWrite(WriteData write, PipedOutputStream out) {
- super();
- this.write = write;
- this.out = out;
- }
- @Override public void run() {
- write.writeMethod(out);
- }
- }
- public class ThreadRead extends Thread {
- private ReadData read;
- private PipedInputStream input;
- public ThreadRead(ReadData read, PipedInputStream input) {
- super();
- this.read = read;
- this.input = input;
- }
- @Override public void run() {
- read.readMethod(input);
- }
- }
- public class Run {
- public static void main(String[] args) {
- try {
- WriteData writeData = new WriteData();
- ReadData readData = new ReadData();
- PipedInputStream inputStream = new PipedInputStream();
- PipedOutputStream outputStream = new PipedOutputStream();
- // inputStream.connect(outputStream);
- outputStream.connect(inputStream); //关键
- ThreadRead threadRead = new ThreadRead(readData, inputStream);
- threadRead.start();
- Thread.sleep(2000);
- ThreadWrite threadWrite = new ThreadWrite(writeData, outputStream);
- threadWrite.start();
- } catch(IOException e) {
- e.printStackTrace();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- try {
- MyThread threadTest = new MyThread();
- threadTest.start();
- threadTest.join();
- System.out.println("threadTest对象执行完,我再执行");
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public class ThreadB extends Thread {
- @Override public void run() {
- try {
- ThreadA a = new ThreadA();
- a.start();
- a.join();
- System.out.println("线程B在run end处打印了");
- } catch(InterruptedException e) {
- System.out.println("线程B在catch处打印了");
- e.printStackTrace();
- }
- }
- }
- public final synchronized void join(long millis) throws InterruptedException {
- long base = System.currentTimeMillis();
- long now = 0;
- if (millis < 0) {
- throw new IllegalArgumentException("timeout value is negative");
- }
- if (millis == 0) {
- while (isAlive()) {
- wait(0);
- }
- } else {
- while (isAlive()) {
- long delay = millis - now;
- if (delay <= 0) {
- break;
- }
- wait(delay);
- now = System.currentTimeMillis() - base;
- }
- }
- }
- public void run() {
- try {
- for (int i = 0; i < 100; i++) {
- if (Tools.tl.get() == null) {
- Tools.tl.set("ThreadA" + (i + 1));
- } else {
- System.out.println("ThreadA get Value=" + Tools.tl.get());
- }
- Thread.sleep(200);
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- for (int i = 0; i < 100; i++) {
- if (Tools.tl.get() == null) {
- Tools.tl.set("ThreadB" + (i + 1));
- } else {
- System.out.println("ThreadB get Value=" + Tools.tl.get());
- }
- Thread.sleep(200);
- }
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- public class Tools {
- public static ThreadLocal tl = new ThreadLocal();
- }
- public class ThreadLocalExt extends ThreadLocal {@Override protected Object initialValue() {
- return new Date().getTime();
- }
- }
- public class InheritableThreadLocalExt extends InheritableThreadLocal {@Override protected Object initialValue() {
- return new Date().getTime();
- }
- }
- public static void main(String[] args) {
- try {
- for (int i = 0; i < 10; i++) {
- System.out.println(" 在Main线程中取值=" + Tools.tl.get());
- Thread.sleep(100);
- }
- Thread.sleep(5000);
- ThreadA a = new ThreadA();
- a.start();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- //main线程和A线程输出的一样
来源: http://www.cnblogs.com/-new/p/7217844.html