什么是多线程?多线程与一般的程序不同,一般的程序都是单线顺序执行的,而多线程则是多个程序同时执行。一般的程序就是一个干十个人的活,而多线程则是十个人干十个人的活;
image.png
-
多线程的两种实现方法
package code;public class Textthread { public static void main(String[] args) { Thread coding = new Coding(); Thread print = new Thread(new Print()); coding.start(); print.start(); System.out.println("你好啊"); } } class Coding extends Thread { public void run() { for (int i = 0; i < 100; i++) { System.out.println("小明打了" + i + "行代码"); try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Print implements Runnable { public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i < 200; i++) { System.out.println("小明打印了" + i + "页材料"); try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.out.println("共用时" + (endTime -startTime) + "ms"); } }
-
线程的等待
public class ProducerAndConsumer { 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{ 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){ System.out.println("Producer: 盒子非空,进入等待队列"); try { box.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } box.boxValue = i; System.out.println("盒子中放入了"+i+",并通知其他等待者"); box.notify(); } } } } class Consumer extends Thread{ 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){ System.out.println("Consumer: 盒子为空,进入等待队列"); try { box.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } box.boxValue -= i; System.out.println("从盒子取出了"+i+",并通知其他等待者"); box.notify(); } } } }
-
锁的实现
public class ThreadAccount {public static void main(String[] args) { BankAccount account = new BankAccount(); Thread husband = new BankThread(account); Thread wife = new BankThread(account); husband.start(); wife.start(); } } class BankAccount { private double balance = 1000; public boolean isSuccess(double add){ if(add<=0){ return false; }else{ synchronized(this){ System.out.println("当前的余额为:"+balance); try { Thread.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } balance += add; System.out.println("新的余额为:"+balance); } return true; } } } class BankThread extends Thread{ BankAccount account; public BankThread(BankAccount account){ this.account = account; } public void run(){ account.isSuccess(200); } }
-
死锁
public class DeadLock { public static void main(String[] args) { Object lock1 = new Object(); Object lock2 = new Object(); Thread thread1 = new ThreadA(lock1,lock2); Thread thread2 = new ThreadB(lock1,lock2); thread1.start(); thread2.start(); } } class ThreadA extends Thread{ Object lock1; Object lock2; public ThreadA(Object lock1, Object lock2){ this.lock1 = lock1; this.lock2 = lock2; } public void run(){ synchronized(lock1){ System.out.println("A拿到了lock1"); try { Thread.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(lock2){ System.out.println("A拿到了lock1和lock2"); } } } } class ThreadB extends Thread{ Object lock1; Object lock2; public ThreadB(Object lock1, Object lock2){ this.lock1 = lock1; this.lock2 = lock2; } public void run(){ synchronized(lock2){ System.out.println("B拿到了lock2"); try { Thread.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(lock1){ System.out.println("B拿到了lock1和lock2"); } } } }
网友评论