</pre>1、多线程的通信、线程的阻塞,唤醒 wait / notify<p></p><p>一个线程切换到另一个线程</p><p></p><pre code_snippet_id="363303" snippet_file_name="blog_20140526_1_2581865" name="code" class="java">package cn.sinobest.jzpt.minidemo.multithreading;
/**
* 两个线程来回切换,唤醒,
* @author : chenhaipeng
* @date : 2014/04/29 14:10
*/
public class TraditionalThreadComnunication {
public static void main(String[] args) {
final Business business = new Business();
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 1; i<=50; i++){
// synchronized (TraditionalThreadComnunication.class){
// for(int j=0; j<10; j++){
// System.out.println("sub thread sequence of "+ j);
// }
// }
business.sub(i);
}
}
}).start();
for(int i = 1; i<=50; i++){
business.main(i);
}
// for(int i = 0; i<50; i++){
// synchronized (TraditionalThreadComnunication.class){
// for(int j=0; j<10; j++){
// System.out.println("main thread sequence of "+ j);
// }
// }
//
// }
// new Thread(new Runnable() {
// @Override
// public void run() {
//
// }
// }).start();
}
}
class Business{
private boolean bShouldSub = true;
public synchronized void sub(int i){
while(!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j = 1; j<=10; j++){
System.out.println("sub thread of "+ j +", loop of "+ i);
}
bShouldSub = false;
this.notify();
}
public synchronized void main(int i){
while(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j = 1; j<=100; j++){
System.out.println("main thread of "+ j +", loop of "+ i);
}
bShouldSub = true;
this.notify();
}
}
2、线程间的数据共享、各自存取自己的数据
package cn.sinobest.jzpt.minidemo.multithreading;
import java.util.Random;
/**
* 利用jdk自带ThreadLocal
* @author : chenhaipeng
* @date : 2014/04/29 20:35
*/
public class ThreadLocalTest {
// private static int data = 0;
// private static Map<Thread,Integer> threadData = new HashMap<Thread, Integer>();
// private 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);
MyThreadScopeData.getInstance().setData(data + "");
MyThreadScopeData.getInstance().setName("name" + data);
new A().get();
new B().get();
}
}).start();
}
}
static class A{
public void get(){
// int data = threadLocal.get();
String data = MyThreadScopeData.getInstance().getData();
System.out.println("A from "+Thread.currentThread().getName()+
" has get data :"+ data);
}
}
static class B{
public void get(){
String data = MyThreadScopeData.getInstance().getData();
System.out.println("B from "+Thread.currentThread().getName()+
" has get data :"+ data);
}
}
}
class MyThreadScopeData{
private MyThreadScopeData(){}
private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
// private static MyThreadScopeData instance = null;
public static MyThreadScopeData getInstance(){
MyThreadScopeData instance = map.get();
if(instance == null){
instance = new MyThreadScopeData();
map.set(instance);
}
return instance;
}
private String name;
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、多线程对共享数据的操作
package cn.sinobest.jzpt.minidemo.multithreading;
/**
* 多线程对共享数据的操作
* @author : chenhaipeng
* @date : 2014/05/11 16:41
*/
public class MultiThreadShareData {
private static ShareData1 data1 = new ShareData1();
public static void main(String[] args) {
ShareData1 data2 = new ShareData1();
new Thread(new MyRunnable1(data2)).start();
new Thread(new MyRunnable2(data2)).start();
final ShareData1 data1 = new ShareData1();
new Thread(new Runnable(){
@Override
public void run() {
data1.decrement();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
data1.increment();
}
}).start();
}
}
class MyRunnable1 implements Runnable{
private ShareData1 data1;
public MyRunnable1(ShareData1 data1){
this.data1 = data1;
}
public void run() {
System.out.println("before**********"+data1.getJ());
data1.decrement();
System.out.println("after**********"+data1.getJ());
}
}
class MyRunnable2 implements Runnable{
private ShareData1 data1;
public MyRunnable2(ShareData1 data1){
this.data1 = data1;
}
public void run() {
System.out.println("before**********"+data1.getJ());
data1.increment();
System.out.println("after**********"+data1.getJ());
}
}
class ShareData1 /*implements Runnable*/{
/* private int count = 100;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
count--;
}
}*/
private int j = 0;
public ShareData1(){
j=100;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public synchronized void increment(){
j++;
}
public synchronized void decrement(){
j--;
}
}