Skip to content

1.多线程实现的方式

1.1 继承Thread类

需要重写run方法

java
public class Thread_One extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
    }
}

Demo类

java
   public static void main(String[] args) {
        Thread_One thread1 =new Thread_One();
        Thread_One thread2=new Thread_One();

        thread1.start();
        thread2.start();
    }

1.2实现Runnable接口

实现Runnable接口,重写run()方法

java
public class Thread_Two implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

Demo类,需要创建Thread类对象

java
    public static void main(String[] args) {
        Thread_Two runnable =new Thread_Two();

        //创建Thread类对象
        Thread thread1=new Thread(runnable,"线程一");
        Thread thread2=new Thread(runnable,"线程二");

        thread1.start();
        thread2.start();

    }

2.获取和设置线程名称的方法

  • void setName(String name) :将此线程名称更改为name
  • String getName():返回此线程名称
  • static Thread currentThread():返回当前正在执行的线程对象的引用

3.获取线程优先级的方法

  • public final void setPriority(int newPriority):更改此线程的优先级
  • public final int getPriority():返回此线程的优先级

4.线程控制

  • static void sleep(long millis):使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行)

  • void join():等待这个线程死亡,此线程完成后其他线程可以执行

  • void setDaemon(boolean on):将此线程标记为守护线程或用户线程,当运行的都是守护线程时,java虚拟机将退出。主线程运行完成,运行结束。

  • 用Thread.currentThread().setName(“ ”):设置主线程

5.同步代码块

锁多条语句操作共享数据,一次只能有一个线程执行

  • 格式:synchronized(任意对象) {多条语句操作共享数据的代码} ----任意对象可以看做锁

6. 同步方法

  • 同步方法,锁的对象是this

    格式:修饰符 synchronized 返回值类型 方法名 (方法参数)

  • 同步静态方法,锁的对象是类名.class

    格式:修饰符 static synchronized 返回值类型 方法名(方法参数)

7.线程安全类

  • StringBuffer
  • Vector
  • Hashtable

Collections有线程安全方法

8.Lock锁

Lock 是接口,通过实现类 Reentrsntlock 进行实例化

  • void lock(); 获得锁

  • void unlock(); 释放锁

9.生产者消费者模式

在Object中

  • void wait():导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法。
  • void notify():唤醒正在等待对象监视器的单个线程
  • void notifyall():唤醒正在等待对象监视器的所有线程

10.卖票案例

java
public class SellTickets implements Runnable {
    private int tickets = 100;
    private Object object = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (object) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在售卖第" + tickets + "张票");
                    tickets--;
                }
            }
        }

    }
}
java
    public static void main(String[] args) {
        SellTickets st=new SellTickets();

        Thread thread1=new Thread(st,"窗口一");
        Thread thread2=new Thread(st,"窗口二");
        Thread thread3=new Thread(st,"窗口三");

        thread1.start();
        thread2.start();
        thread3.start();

    }