有2中方法可以來創(chuàng)建線程:
- 繼承Thread類
- 用一個類實現(xiàn)Runnable接口
具體如下:
package com.qingke.thread;
public class LeannThread {
public static void main(String[] args) {
Thread code = new Code();
code.setName("編寫");
Print print = new Print();
Thread pr = new Thread(print, "打印機");
code.start();
pr.start();
}
}
class Code extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(this.getName() + ":" + "小明在寫第" + i + "行代碼");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("代碼寫完!");
}
}
class Print implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 15; i++) {
System.out.println(Thread.currentThread().getName() + ":" + "小明在打印第" + i + "頁材料");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("打印完!");
}
}
在對于像銀行賬戶的需要同步的問題上谋币,現(xiàn)在有一個例子:
一對夫妻在銀行有一個共同的賬戶完箩,則需要用到同步使得賬戶中的余額保持同步杈绸。
如下:
package com.qingke.thread;
public class BankThread {
public static void main(String[] args) {
BankAccount count = new BankAccount();
Thread husband = new BankedThread(count);
Thread wife = new BankedThread(count);
husband.start();
wife.start();
}
}
class BankAccount {
private double balance = 1000;
public /*synchronized*/boolean deposit(double newAdd) {
if (balance <= 0) {
return false;
} else {
synchronized (this) {
System.out.println("當(dāng)前余額為" + balance);
balance += newAdd;
System.out.println("當(dāng)前余額為" + balance);
}
return true;
}
}
}
class BankedThread extends Thread {
private BankAccount bankAccount;
public BankedThread(BankAccount count) {
bankAccount = count;
}
public void run() {
bankAccount.deposit(200);
}
}
在上述代碼中synchronized 可以直接放在方法聲明上,也可以寫在方法塊內(nèi)synchronized(obj){} * ** obj*表示需要同步的對象瞬矩。
模擬一種場景评也,假設(shè)有一個生產(chǎn)商Producer和一個消費者Consumer炼杖,另外有一個容器Box戈鲁,Box中只能放一樣?xùn)|西。若Box為空嘹叫,則要等Producer生產(chǎn)出來再放到Box中,在通知Consumer诈乒。若Box中不空罩扇,則需要等Consumer拿完,在通知Producer怕磨。
代碼如下:
package com.qingke.thread;
public class LearnThreadCommunication {
public static void main(String[] args) {
Box box = new Box();
Thread producer = new Producer(box);
Thread consumer = new Consumer(box);
producer.start();
consumer.start();
}
}
class Box {
public int boxValue = 0;
}
class Producer extends Thread {
private Box box;
public Producer(Box box) {
this.box = box;
}
public void run() {
for (int i = 1; i < 6; i++) {
synchronized (box) {
while (box.boxValue != 0) {
try {
System.out.println("Producer: Box是滿的喂饥,等待");
box.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
box.boxValue = i;
System.out.println("Producer: Box中放入了" + i + ",并通知其他等待者");
box.notify();
}
}
}
}
class Consumer extends Thread {
private Box box;
public Consumer(Box box) {
this.box = box;
}
public void run() {
for (int i = 1; i < 6; i++) {
synchronized (box) {
while (box.boxValue == 0) {
try {
System.out.println("Consumer: Box是空的,等待");
box.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
box.boxValue = 0;
System.out.println("Consumer: Box中取出了" + i + ",并通知供應(yīng)者");
box.notify();
}
}
}
}
用到Object的 wait()和notify()方法