public class MyThread extends Thread {
public MyThread() {}
public MyThread(String name){
super(name);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+":"+i);
}
}
}
public class MyThreadDemo {
public static void main(String[] args) {
MyThread myThread1 = new MyThread();
MyThread myThread2 = new MyThread();
myThread1.start();
myThread2.start();
MyThread myThread3 = new MyThread("线程1");
myThread3.start();
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadPriority extends Thread {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(getName() + "-" + i);
}
}
}
public class ThreadPriorityDemo {
public static void main(String[] args) {
ThreadPriority threadPriority1 = new ThreadPriority();
ThreadPriority threadPriority = new ThreadPriority();
threadPriority.setName("线程3");
threadPriority1.setName("线程1");
System.out.println(threadPriority.getName()+"优先级:"+threadPriority.getPriority());
System.out.println(threadPriority1.getName()+"优先级:"+threadPriority1.getPriority());
System.out.println(Thread.MAX_PRIORITY);
System.out.println(Thread.MIN_PRIORITY);
System.out.println(Thread.NORM_PRIORITY);
threadPriority.setPriority(1);
threadPriority1.setPriority(3);
threadPriority.start();
threadPriority1.start();
}
}
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class MyRunnableDemo {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
// Thread thread = new Thread(myRunnable);
// Thread thread1 = new Thread(myRunnable);
Thread thread = new Thread(myRunnable, "张三");
Thread thread1 = new Thread(myRunnable, "李四");
thread.start();
thread1.start();
}
}
public class SellTicket implements Runnable {
private int tickets = 100;
@Override
public void run() {
//死循环,一直处于卖票状态
while (true) {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
tickets--;
}
}
}
}
public class SellTicketDemo {
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
public class SellTicket implements Runnable {
private int tickets = 100;
private Object obj = new Object();
@Override
public void run() {
while (true) {
synchronized (obj){
if (tickets > 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
tickets--;
}
}
}
}
}
public class SellTicketDemo {
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
public class SellTicket implements Runnable {
private int tickets = 100;
private Lock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖第" + tickets + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
}
public class SellTicketDemo {
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
public class Box {
private int milk;
private boolean state = false;
public synchronized void put(int milk) {
//如果有牛奶,就是当前线程等待
if(state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有牛奶
this.milk = milk;
System.out.println("送奶工放入第" + this.milk + "瓶奶");
state = true;
notifyAll();
}
public synchronized void get() {
//如果没有牛奶,使当前线程等待
if(!state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果有牛奶
System.out.println("拿到第" + this.milk + "瓶奶");
state = false;
notifyAll();
}
}
public class Producer implements Runnable {
private Box b;
public Producer(Box box) {
this.b = box;
}
@Override
public void run() {
for (int i = 1; i < 5; i++) {
b.put(i);
}
}
}
public class Customer implements Runnable {
private Box b;
public Customer(Box box) {
this.b = box;
}
@Override
public void run() {
while(true){
b.get();
}
}
}
public class BoxDemo {
public static void main(String[] args) {
Box b = new Box();
Producer producer = new Producer(b);
Customer customer = new Customer(b);
Thread thread1 = new Thread(producer);
Thread thread2 = new Thread(customer);
thread1.start();
thread2.start();
}
}