Runnable
创建两个线程
package Threading;
public class Myrunable implements Runnable{
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
package Threading;
public class test {
public static void main(String[] args) {
Myrunable my = new Myrunable();
Thread t1 = new Thread(my,"飞机");
Thread t2 = new Thread(my,"火车");
t1.start();
t2.start();
}
}
卖票案例
package SellTicket;
public class SellTicket implements Runnable{
private int ticket = 100;
@Override
public void run() {
while(true){
if(ticket>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}
}
}
package SellTicket;
public class test {
public static void main(String[] args) {
SellTicket st = new SellTicket();
Thread t1 = new Thread(st,"窗口1");
Thread t2 = new Thread(st,"窗口2");
Thread t3 = new Thread(st,"窗口3");
t1.start();
t2.start();
t3.start();
}
}
package SellTicket;
public class SellTicket implements Runnable{
private int ticket = 100;
private Object obj =new Object();
@Override
public void run() {
while(true){
synchronized (obj){
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}
}
}
}

Lock
package SellTicket;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SellTicket implements Runnable{
private int ticket = 100;
private Lock lock = new ReentrantLock();
@Override
public void run() {
while(true){
try{
lock.lock();
if(ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}finally {
lock.unlock();
}
}
}
}
生产者消费者问题
package prosellquestion;
public class Box {
private int milk;
private boolean status = false;
public synchronized void put(int milk){
if(status){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.milk=milk;
System.out.println("生产者生产了第"+this.milk+"瓶奶");
status=true;
notifyAll();
}
public synchronized void get(){
if(!status){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者拿到了第"+this.milk+"瓶奶");
status=false;
notifyAll();
}
}
package prosellquestion;
public class producer implements Runnable{
private Box box;
public producer(Box box) {
this.box=box;
}
@Override
public void run() {
while (true){
box.get();
}
}
}
package prosellquestion;
public class seller implements Runnable{
private Box box;
public seller(Box box) {
this.box= box;
}
@Override
public void run() {
for(int i=1;i<50;i++){
box.put(i);
}
}
}
package prosellquestion;
public class test {
public static void main(String[] args) {
Box box = new Box();
producer p = new producer(box);
seller s = new seller(box);
Thread p1 = new Thread(p,"生产者");
Thread s1 = new Thread(s,"消费者");
p1.start();
s1.start();
}
}
生产者消费者改进
package prosellquestion;
import java.util.Random;
public class Box {
private int lenght = 0;
private static int bufferlenght=50;
private boolean status = false;
Random ra = new Random();
public synchronized void put(){
int r=ra.nextInt(bufferlenght);
int orlen=0;
if(status||lenght>=bufferlenght){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(lenght+r>bufferlenght){
orlen=bufferlenght-lenght;
lenght=bufferlenght;
System.out.println("生产者生产了"+r+"个文件,但超出缓冲区,只存入"+orlen+"个文件,"+"缓冲区中还剩"+lenght);
}
else {
lenght+=r;
System.out.println("生产者生产了"+r+"个文件,还剩"+lenght);
}
status=true;
notify();
}
public synchronized void get(){
int r=ra.nextInt(bufferlenght);
int orlen=0;
if(!status||lenght<=0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(lenght-r<0){
orlen = lenght;
lenght=0;
System.out.println("消费者消费"+r+"个文件,但缓冲区中文件不足,只消费了"+orlen+"个文件,"+"缓冲区中还剩"+lenght);
}
else {
lenght-=r;
System.out.println("消费者消费了"+r+"个文件,还剩"+lenght);
}
status=false;
notify();
}
}
package prosellquestion;
public class seller implements Runnable{
private Box box;
public seller(Box box) {
this.box= box;
}
@Override
public void run() {
while (true){
box.put();
}
}
}
package prosellquestion;
public class producer implements Runnable{
private Box box;
public producer(Box box) {
this.box=box;
}
@Override
public void run() {
while (true){
box.get();
}
}
}
package prosellquestion;
public class test {
public static void main(String[] args) {
Box box = new Box();
producer p = new producer(box);
seller s = new seller(box);
Thread p1 = new Thread(p,"生产者");
Thread s1 = new Thread(s,"消费者");
p1.start();
s1.start();
}
}