一个进程中需要多个控制流程时,就用到了线程。进程运行时开辟了一块地址空间,一个进程下的多个线程共享这块地址空间,可以数据共享,互相通讯。
一、创建线程的两种方式
实现Runnable接口
public class MyRunnable implements Runnable {
@Override
public void run() {
for(int i = 0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"i:"+i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
继承Thread类
public class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
public void run() {
for(int i = 0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"i:"+i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试
public class ThreadDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread("线程一");
MyThread thread2 = new MyThread("线程二");
MyThread thread3 = new MyThread("线程三");
thread1.start();
thread2.start();
thread3.start();
MyRunnable myRunnable = new MyRunnable();
Thread thread4 = new Thread(myRunnable,"线程4");
thread4.start();
}
}
二、锁的理解,这里以生产者消费者举例。生产者往仓库里加商品,消费者从仓库里取商品。这里的仓库就是锁。
仓库:
package fuxi01_thread;
import java.util.ArrayList;
import java.util.List;
public class CangKu {
private List<String> cangku = new ArrayList<String>();
private int size = 20;
public String remove() {
String s = "";
if(cangku.size() > 0) {
s = cangku.remove(0);
}
return s;
}
public boolean add(String product) {
boolean flg;
if(cangku.size() > size) {
flg = false;
}else {
flg = cangku.add(product);
}
System.out.println("仓库中商品数量:"+cangku.size());
return flg;
}
}
生产者:
package fuxi01_thread;
import java.util.Random;
public class ShengChanZhe implements Runnable {
private CangKu cangku;
public ShengChanZhe(CangKu cangku) {
this.cangku = cangku;
}
@Override
public void run() {
while(true) {
synchronized (cangku) {
String product = "商品:"+new Random().nextInt(100);
boolean flg= cangku.add(product);
if(flg) {
System.out.println(Thread.currentThread().getName()+"添加商品:"+product);
}
try {
cangku.notifyAll();
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
消费者:
package fuxi01_thread;
import java.util.Random;
public class XiaoFeiZhe implements Runnable {
private CangKu cangku;
public XiaoFeiZhe(CangKu cangku) {
this.cangku = cangku;
}
@Override
public void run() {
while(true) {
synchronized (cangku) {
String s = cangku.remove();
if(!"".equals(s)) {
System.out.println(Thread.currentThread().getName()+"买走了商品:"+s);
}else {
try {
cangku.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
测试类:
package fuxi01_thread;
public class Test {
public static void main(String[] args) {
CangKu cangKu = new CangKu();
ShengChanZhe shengChanZhe1 = new ShengChanZhe(cangKu);
ShengChanZhe shengChanZhe2 = new ShengChanZhe(cangKu);
XiaoFeiZhe xiaoFeiZhe1 = new XiaoFeiZhe(cangKu);
XiaoFeiZhe xiaoFeiZhe2 = new XiaoFeiZhe(cangKu);
XiaoFeiZhe xiaoFeiZhe3 = new XiaoFeiZhe(cangKu);
new Thread(shengChanZhe1,"生产者1").start();
new Thread(shengChanZhe2,"生产者2").start();
new Thread(xiaoFeiZhe1,"消费者1").start();
new Thread(xiaoFeiZhe2,"消费者2").start();
new Thread(xiaoFeiZhe3,"消费者3").start();
}
}