1.生产者,消费者线程。
package com.test;
/**
* Created by ll on 2017/10/6.
*/
public class PC {
public static void main(String[] args) {
Shared shared=new Shared();
new Producer(shared).start();
new Consumer(shared).start();
}
}
class Shared {
private char c;
private volatile boolean writeable = true;
synchronized void setSharedChar(char c) {
while (!writeable) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.c = c;
writeable = false;
notify();
}
synchronized char getSharedChar() {
while (writeable) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writeable = true;
notify();
return c;
}
}
class Producer extends Thread{
private final Shared shared;
Producer(Shared shared) {
this.shared = shared;
}
@Override
public void run() {
super.run();
for (char ch = 'A'; ch <='Z'; ch++) {
synchronized (shared){
shared.setSharedChar(ch);
System.out.println(ch+"produced by produced");
}
}
}
}
class Consumer extends Thread{
private final Shared shared;
Consumer(Shared shared) {
this.shared = shared;
}
@Override
public void run() {
super.run();
char ch;
do {
synchronized (shared){
ch= shared.getSharedChar();
System.out.println(ch+"consumed by consumer.");
}
} while (ch!='Z');
}
}
改进:
package com.test;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by ll on 2017/10/6.
*/
public class PC {
public static void main(String[] args) {
Shared shared=new Shared();
new Producer(shared).start();
new Consumer(shared).start();
}
}
class Shared {
private char c;
private volatile boolean available;
private final Lock lock;
private final Condition condition;
Shared() {
available=false;
lock=new ReentrantLock();
condition=lock.newCondition();
}
Lock getLock(){
return lock;
}
void setSharedChar(char c) {
lock.lock();
try {
while (available)
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.c = c;
available = true;
condition.signal();
} finally {
lock.unlock();
}
}
char getSharedChar() {
lock.lock();
try {
while (!available)
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
available = false;
condition.signal();//唤醒一个等待中的线程
} finally {
lock.unlock();
return c;
}
}
}
class Producer extends Thread{
private final Shared shared;
private final Lock l;
Producer(Shared shared) {
this.shared = shared;
l=shared.getLock();
}
@Override
public void run() {
super.run();
for (char ch = 'A'; ch <='Z'; ch++) {
l.lock();
shared.setSharedChar(ch);
System.out.println(ch+"produced by produced");
l.unlock();
}
}
}
class Consumer extends Thread{
private final Shared shared;
private final Lock l;
Consumer(Shared shared) {
this.shared = shared;
l=shared.getLock();
}
@Override
public void run() {
super.run();
char ch;
do {
l.lock();
ch= shared.getSharedChar();
System.out.println(ch+"consumed by consumer.");
l.unlock();
} while (ch!='Z');
}
}
再次改进:
package com.ll.test;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by ll on 2017/10/6.
*/
public class PC {
public static void main(String[] args) {
final BlockingQueue<Character> bq;
bq= new ArrayBlockingQueue<Character>(26);
final ExecutorService executorService= Executors.newFixedThreadPool(2);
Runnable producer=()->{
for (char ch='A';ch<='Z';ch++){
try {
bq.put(ch);
System.out.println("produced by producer."+ch);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
executorService.execute(producer);
Runnable consumer=()->{
char ch='\0';
do {
try {
ch=bq.take();
System.out.println("consumerd by consumer."+ch);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (ch!='Z');
executorService.shutdownNow();
};
executorService.execute(consumer);
}
}
注意:Fork/Join框架。
2.ThreadLocal。
public class ThreadLoclDemo {
private static volatile ThreadLocal<String> uid=new ThreadLocal<>();
public static void main(String[] args) {
Runnable runnable=()->{
String name=Thread.currentThread().getName();
if (name.equals("A")){
uid.set("AThread");
}else{
uid.set("otherThread");
}
System.out.println(name+" "+uid.get());
};
Thread thdA=new Thread(runnable);
thdA.setName("A");
Thread thdB=new Thread(runnable);
thdB.setName("B");
thdA.start();
thdB.start();
}
}
3.InheritableThreadLocal。
public class ThreadThanfer {
private static final InheritableThreadLocal<Integer> inVal=new InheritableThreadLocal<>();
public static void main(String[] args) {
Runnable runnable=()->{
inVal.set(new Integer(100));
Runnable runnable1=()->{
Thread thd=Thread.currentThread();
String name=thd.getName();
System.out.println(name+":"+inVal.get());
};
Thread thdChild=new Thread(runnable1);
thdChild.setName("Child");
thdChild.start();
};
new Thread(runnable).start();
}
}
4.Executor。
public class ExecutorTest {
public static void main(String[] args) {
Runnable runnable=()->{
String name=Thread.currentThread().getName();
int count=0;
while (true)
System.out.println(name+" "+count++);
};
ExecutorService ex= Executors.newFixedThreadPool(2);
ex.submit(runnable);
ex.submit(runnable);
}
}
改进:
public class ExecutorTest {
public static void main(String[] args) {
Runnable runnable=()->{
String name=Thread.currentThread().getName();
int count=0;
while (true)
System.out.println(name+" "+count++);
};
/* ExecutorService ex= Executors.newFixedThreadPool(2);
ex.submit(runnable);
ex.submit(runnable);*/
ExecutorService es=Executors.newSingleThreadExecutor(new NamedThread("A"));
es.submit(runnable);
es=Executors.newSingleThreadExecutor(new NamedThread("B"));
es.submit(runnable);
}
}
class NamedThread implements ThreadFactory{
private volatile String name;
public NamedThread(String name) {
this.name = name;
}
@Override
public Thread newThread(Runnable r) {
return new Thread(r,name);
}
}