1、继承实现方式
public static void main(String[] args) {
MyThread mt1 = new MyThread("科比");
MyThread mt2 = new MyThread("麦迪");
MyThread mt3 = new MyThread("艾弗森");
MyThread mt4 = new MyThread("卡特");
mt1.start();
mt2.start();
mt3.start();
mt4.start();
}
public class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
public MyThread() {
super();
}
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.println(Thread.currentThread().getName()+"正在打出第"+ i +"招");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
2、实现 Runnable 接口
public static void main(String[] args) {
MyRunnable mr1 = new MyRunnable();
Thread t1 = new Thread(mr1,"线程1");
Thread t2 = new Thread(mr1,"线程2");
Thread t3 = new Thread(mr1,"线程3");
t1.start();
t2.start();
t3.start();
}
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + "i=" + i);
}
}
}
3、setDaemon
public static void main(String[] args) {
MyThread mt1 = new MyThread("科比");
MyThread mt2 = new MyThread("艾弗森");
MyThread mt3 = new MyThread("麦迪");
MyThread mt4 = new MyThread("卡特");
mt1.setDaemon(true);
mt2.setDaemon(true);
mt3.setDaemon(true);
mt4.setDaemon(true);
mt1.start();
mt2.start();
mt3.start();
mt4.start();
for (int i = 0; i < 3; i++) {
System.out.println("涅尘也在说话");
}
System.out.println("别说了");
}
public class MyThread extends Thread{
public MyThread() {
super();
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
String[] songs = {
"随风倒十分",
"按时发放",
"让她以后就业报告",
"施工方皇家银行那边",
"是德国",
"是如何将也很难",
"还有不规范",
"贵宾套房从",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
"交通银行日方对此",
};
for (int i = 0; i < songs.length; i++) {
System.out.println(Thread.currentThread().getName() + "正在唱" + songs[i]);
}
}
}
4、join
public static void main(String[] args) throws InterruptedException {
MyThread mt1 = new MyThread("科比");
MyThread mt2 = new MyThread("麦迪");
MyThread mt3 = new MyThread("艾弗森");
MyThread mt4 = new MyThread("卡特");
mt1.start();
mt1.join();//先执行调用join的线程 才能执行别的
mt2.start();
mt3.start();
mt4.start();
}
public class MyThread extends Thread{
public MyThread() {
super();
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
String[] songs = {
"随风倒十分",
"按时发放",
"让她以后就业报告",
"施工方皇家银行那边",
"是德国",
"是如何将也很难",
"还有不规范",
"贵宾套房从",
"交通银行日方对此",
};
for (int i = 0; i < songs.length; i++) {
System.out.println(Thread.currentThread().getName() + "正在唱" + songs[i]);
}
}
}
5、买票、买票
public static void main(String[] args) {
SellTickets s1 = new SellTickets();
Thread t1= new Thread(s1,"窗口1");
Thread t2= new Thread(s1,"窗口2");
Thread t3= new Thread(s1,"窗口3");
t1.start();
t2.start();
t3.start();
}
public class SellTickets implements Runnable{
private int tickets = 100;//一共100张票
@Override
public void run() {
while (true){
//同步语句块 synchronized
synchronized (this){
//还有没有票
if (this.tickets > 0){
System.out.println(Thread.currentThread().getName() + "正在出售第" + this.tickets + "张");
this.tickets--;
}
}
//间隔时间
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
6、生产者、消费者
public static void main(String[] args) {
Box box = new Box(); //要操作的奶箱对象
//生产者
Producer producer = new Producer(box);
//消费者
Customer customer = new Customer(box);
Thread thread1 = new Thread(producer);
Thread thread2 = new Thread(customer);
thread1.start();
thread2.start();
}
public class Box {
private int milk;//第几瓶牛奶
private boolean state = false; //奶箱的状态 初始化为空 flase
/**
* 放入牛奶
* @param milk
*/
public synchronized void put(int milk){
if (state){ //如果不是空的 就等待
try {
//线程等待 需要有人唤醒才会继续向下进行
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//如果没有牛奶 就放入牛奶
this.milk = milk;
System.out.println("涅尘将第" +this.milk +"瓶放入了奶箱");
//将牛奶改为有
this.state = true;
notifyAll();//此时唤醒的是get方法里的等待进程
}
/**
* 取走牛奶
*/
public synchronized void get(){
if (!state){//如果奶箱里没有牛奶,就等待
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//如果有牛奶 就拿走牛奶
System.out.println("姜姗将第" + this.milk +"瓶牛奶喝了");
//将奶箱的状态改为空
this.state = false;
//唤醒所有正在等待的线程
notifyAll();//此时唤醒的是put方法里的等待进程
}
}
public class Customer implements Runnable{
private Box box;
public Customer(Box box) {
this.box = box;
}
@Override
public void run() {
while (true){
box.get();
}
}
}
public class Producer implements Runnable{
private Box box;
public Producer(Box box) {
this.box = box;
}
public Producer() {
}
@Override
public void run() {
for (int i = 1; i <= 7; i++) {
box.put(i);
}
}
}