实现Runnable接口
public class ThreadCreate implements Runnable {//使用Runnable接口创建线程
public static void main(String[] args) {
ThreadCreate threadCreate = new ThreadCreate();//创建自定义类的对象
//将自定义类的对象作为参数传给Thread类的构造方法,得到Thread类的对象
Thread thread = new Thread(threadCreate);
//开启子线程
thread.start();
}
@Override
public void run() {//子线程中要运行的代码
System.out.println("我是子线程");
}
}
继承Thread类
public class ThreadCreate2 extends Thread {//使用Thread类的子类创建线程
public static void main(String[] args) {
ThreadCreate2 threadCreate2 = new ThreadCreate2();//创建子线程对象
threadCreate2.start();//开启子线程
}
@Override
public void run() {//子线程中要运行的代码
System.out.println("我是子线程");
}
}
实现Callable接口
如果想要区分不同的线程,那么就需要对象的名称加以区分,就要对线程加以命名,一般线程的命名建议在创建线程对象时或在启动前进行,不建议对已经启动的线程进行命名,也不建议对不同的线程设置相同的名字
实现Runnable接口的线程命名
public class ThreadCreate implements Runnable {//使用Runnable接口创建线程,并改名
public static void main(String[] args) {
ThreadCreate threadCreate = new ThreadCreate();//创建自定义类的对象
//创建子线程对象1,在构造方法中对子线程1进行命名
Thread thread1 = new Thread(threadCreate, "子线程1");
//开启子线程1
thread1.start();
//创建子线程对象2
Thread thread2 = new Thread(threadCreate);
//使用setName()方法对子线程2进行命名
thread2.setName("子线程2");
//开启子线程1
thread2.start();
}
@Override
public void run() {//子线程中要运行的代码,作用是打印当前线程的名称
System.out.println("我是:" + Thread.currentThread().getName());
}
}
Thread类子类的线程的重命名
public class ThreadCreate2 extends Thread {//使用Thread类的子类创建线程,并改名
public ThreadCreate2(String name) {//该构造方法用来对子线程进行命名,内部调用了setName()方法
this.setName(name);
}
public static void main(String[] args) {
ThreadCreate2 threadCreate2 = new ThreadCreate2("子线程");//创建子线程对象并命名
threadCreate2.start();//开启子线程
}
@Override
public void run() {//子线程中要运行的代码
System.out.println("我是" + Thread.currentThread().getName());
}
}
public class ThreadPriority implements Runnable {//线程优先级的设置与获取
@Override
public void run() {//输出打印线程基本信息
System.out.println("线程名称:" +
Thread.currentThread().getName() +
"\t线程优先级:" +
Thread.currentThread().getPriority());
}
public static void main(String[] args) {
ThreadPriority threadPriority = new ThreadPriority();
Thread thread1 = new Thread(threadPriority, "线程1");//创建线程2,重命名并设置优先级
thread1.setPriority(1);
Thread thread2 = new Thread(threadPriority, "线程2");//创建线程2,重命名并设置优先级
thread2.setPriority(10);
thread1.start();//启动线程
thread2.start();
}
}
public class ThreadYield implements Runnable {//线程休眠与线程礼让示例
@Override
public void run() {
try {
Thread.sleep(3000);//线程休眠3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread.yield();//线程礼让
System.out.println("线程礼让与线程休眠示例");
}
public static void main(String[] args) {
ThreadYield threadYield = new ThreadYield();
Thread thread = new Thread(threadYield);
thread.start();
}
}
public class ThreadJoin implements Runnable {//线程联合示例
@Override
public void run() {//打印子线程名称
System.out.println("线程名称:" + Thread.currentThread().getName());
}
public static void main(String[] args) {
ThreadJoin threadJoin = new ThreadJoin();//创建子线程
Thread thread = new Thread(threadJoin, "线程");
thread.start();//启动子线程
try {
thread.join();//让子线程先插队
} catch (InterruptedException e) {
e.printStackTrace();
}
//打印主线程名称
System.out.println("线程名称:" + Thread.currentThread().getName());
}
}
public class ThreadStop implements Runnable {//线程停止示例
private static boolean flag = true;//子线程的停止标识
@Override
public void run() {
while (flag) {//当调用stop()方法时子线程结束
System.out.println("线程名称:" + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
//创建并启动子线程
ThreadStop threadStop = new ThreadStop();
Thread thread = new Thread(threadStop, "子线程");
thread.start();
int i = 0;
while (i++ < 10) {
System.out.println("线程名称:" + Thread.currentThread().getName());
}
ThreadStop.stop();//调用stop()方法结束子线程
}
private static void stop() {//改变停止标识,调用此方法结束子线程
flag = false;
}
}
public class TurtleRabbitRace implements Runnable {//使用Java多线程模拟龟兔赛跑比赛
private String winner = null;
public static void main(String[] args) {
//创建并运行线程
TurtleRabbitRace race = new TurtleRabbitRace();
Thread thread1 = new Thread(race, "兔子");
Thread thread2 = new Thread(race, "乌龟");
thread1.start();
thread2.start();
}
@Override
public void run() {
int i = 0;
while (i++ < 100) {//循环跑100米
if (getWinner()) {//如果已经有获胜者,另一个人就不再跑了,比赛结束
break;
}
System.out.println(Thread.currentThread().getName() + "跑了" + i + "米");//打印每个选手的比赛进度
if (i > 99) {//当有选手跑到100米时,诞生冠军,并且打印冠军的信息
winner = Thread.currentThread().getName();
System.out.println(winner + "是胜利者");
}
if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {//如果比赛选手是兔子,让兔子每10米休息1毫秒
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 用来得到比赛是否结束
*/
private boolean getWinner() {
if (winner != null) {//如果有冠军返回true
return true;
} else {
return false;//如果没有冠军返回false
}
}
}
public class ThreadConflict implements Runnable {//线程冲突示例
//票数,总共100张
private static int ticket = 100;
@Override
public void run() {
while (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖了一张票,还剩" + (--ticket));
}
}
public static void main(String[] args) {
//创建4个子进程,模拟4个售票员
ThreadConflict conflict = new ThreadConflict();
Thread thread1 = new Thread(conflict, "售票员1");
Thread thread2 = new Thread(conflict, "售票员2");
Thread thread3 = new Thread(conflict, "售票员3");
Thread thread4 = new Thread(conflict, "售票员4");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
public class SynchronizedStatements implements Runnable {//同步语句解决线程冲突
//票数
private static int ticket = 100;
@Override
public void run() {
while (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ("") {
if (ticket <= 0) {
break;
}
System.out.println(Thread.currentThread().getName() + "卖了一张票,还剩" + (--ticket));
}
}
}
public static void main(String[] args) {
/**
* 创建子线程对象
*/
SynchronizedStatements conflict = new SynchronizedStatements();
Thread thread1 = new Thread(conflict, "售票员1");
Thread thread2 = new Thread(conflict, "售票员2");
Thread thread3 = new Thread(conflict, "售票员3");
thread1.start();
thread2.start();
thread3.start();
}
}
public class SynchronizedMethods implements Runnable {//同步方法解决线程冲突
//票数
private static int ticket = 100;
@Override
public void run() {
while (ticket > 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
sellTicket();//调用同步方法
}
}
/**
* 同步方法,将可能会发生并发的代码锁起来
*/
private synchronized void sellTicket() {
if (ticket <= 0) {
return;
}
System.out.println(Thread.currentThread().getName() + "卖了一张票,还剩" + (--ticket));
}
public static void main(String[] args) {
/**
* 创建子线程对象
*/
SynchronizedMethods conflict = new SynchronizedMethods();
Thread thread1 = new Thread(conflict, "售票员1");
Thread thread2 = new Thread(conflict, "售票员2");
Thread thread3 = new Thread(conflict, "售票员3");
thread1.start();
thread2.start();
thread3.start();
}
}
public class ThreadDeadLock {//线程死锁示例
public static void main(String[] args) {
Thread thread1 = new Thread(new DeadLockA(), "线程1");
Thread thread2 = new Thread(new DeadLockB(), "线程2");
thread1.start();
thread2.start();
}
}
class DeadLockA implements Runnable {//A锁需要B锁来解锁
@Override
public void run() {
synchronized ("A") {
System.out.println("线程1获得了A锁");
synchronized ("B") {
System.out.println("线程1获得了A锁和B锁");
}
}
}
}
class DeadLockB implements Runnable {//B锁需要A锁来解锁
@Override
public void run() {
synchronized ("B") {
System.out.println("线程2获得了B锁");
synchronized ("A") {
System.out.println("线程2获得了B锁和A锁");
}
}
}
}
public class ThreadCoordination {//线程协调示例
public static void main(String[] args) {
Thread thread1 = new Thread(new DeadLockAA(), "线程1");
Thread thread2 = new Thread(new DeadLockBB(), "线程2");
thread1.start();
thread2.start();
}
}
class DeadLockAA implements Runnable {
@Override
public void run() {
synchronized ("A") {
System.out.println("线程1获得了A锁");
try {
"A".wait();//让该线程的"A"锁等一下,让其他线程先使用
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ("B") {
System.out.println("线程1获得了A锁和B锁");
}
}
}
}
class DeadLockBB implements Runnable {
@Override
public void run() {
synchronized ("B") {
System.out.println("线程2获得了B锁");
synchronized ("A") {
System.out.println("线程2获得了B锁和A锁");
"A".notify();//提醒线程1线程2已经用完了"A"锁内的内容
}
}
}
}
public class ThreadSingleton {//懒汉单例模式示例
public static void main(String[] args) {
//创建100个线程
for (int i = 0; i < 100; i++) {
Thread thread = new Thread(new HelloRunnable(), "线程 " + i);
thread.start();
}
}
}
class Lazybones {
public Lazybones() {
System.out.println("对象实例化了" + Thread.currentThread().getName());
}
private static Lazybones lazybones = null;
public static Lazybones getLazybones() {
//加锁是为了解决多线程不安全问题,只有当当前线程获得对象之后,下一线程才能获得对象
synchronized ("") {
if (lazybones == null) {
lazybones = new Lazybones();
}
}
return lazybones;
}
}
class HelloRunnable implements Runnable {
@Override
public void run() {
//调用方法得到懒汉单例模式类对象
Lazybones.getLazybones();
}
}
public class Product {//产品类
private String name = null;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Producer implements Runnable {//生产者
//成员变量缓冲区
private ProductPool productPool;
//构造方法
public Producer(ProductPool productPool) {
this.productPool = productPool;
}
@Override
public void run() {
int i = 0;
while (true) {
i = i % 10;
String name = "产品" + (i++ + 1);
Product product = new Product(name);
this.productPool.push(product);
System.out.println(Thread.currentThread().getName() + "生产了------------->" + name);
}
}
}
public class Consumer implements Runnable {
public ProductPool productPool;
public Consumer(ProductPool productPool) {
this.productPool = productPool;
}
@Override
public void run() {
int i = 3;
while (i > 0) {
i--;
Product product = this.productPool.pop();
System.out.println(Thread.currentThread().getName() + "消费了----->" + product.getName());
}
}
}
public class ProductPool {//产品池
/**
* 定义缓冲区的大小和缓冲区列表
*/
public int Maxsize;
public List<Product> list;
/**
* 构造方法
*/
public ProductPool(int size, List<Product> list) {
this.Maxsize = size;
this.list = list;
}
/**
* 放产品进缓冲区的方法
*/
public synchronized void push(Product product) {
if (this.list.size() >= Maxsize) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.list.add(product);
this.notifyAll();
}
/**
* 取产品出缓冲区的方法
*/
public synchronized Product pop() {
if (this.list.size() <= 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Product product = this.list.remove(0);
this.notifyAll();
return product;
}
}
public class MainMethod {//主方法
public static void main(String[] args) {
/**
* 实例化缓冲区
*/
ProductPool productPool = new ProductPool(10, new LinkedList<>());
/**
* 创建生产者线程1
*/
Producer producer = new Producer(productPool);
Thread thread = new Thread(producer, "生产者1");
thread.start();
/**
* 创建消费者线程1
*/
Consumer consumer1 = new Consumer(productPool);
Thread thread1 = new Thread(consumer1, "消费者1");
thread1.start();
/**
* 创建消费者线程2
*/
Consumer consumer2 = new Consumer(productPool);
Thread thread2 = new Thread(consumer2, "消费者2");
thread2.start();
/**
* 创建消费者线程3
*/
Consumer consumer3 = new Consumer(productPool);
Thread thread3 = new Thread(consumer3, "消费者3");
thread3.start();
}
}
public class ThreadCallable implements Callable {//Callable接口的线程对象示例
int i = 0;
@Override
public Object call() throws Exception {
for (int i1 = 0; i1 < 10; i1++) {
System.out.println(Thread.currentThread().getName() + ":" + i++);
}
return i;//获得子线程的返回值
}
public static void main(String[] args) {
ThreadCallable threadCallable = new ThreadCallable();
for (int i = 0; i < 10; i++) {//创建10个子线程
FutureTask futureTask = new FutureTask(threadCallable);
Thread thread = new Thread(futureTask, "子线程" + i);
thread.start();
try {//打印子线程的返回值
System.out.println("子线程" + i + "返回值:" + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
public class ThreadLock implements Runnable {
//票数
private static int ticket = 100;
private ReentrantLock reentrantLock = new ReentrantLock();
@Override
public void run() {
while (ticket > 0) {
//上锁
reentrantLock.lock();
if (ticket <= 0) {
reentrantLock.unlock();
return;
}
System.out.println(Thread.currentThread().getName() + "卖了一张票,还剩" + (--ticket));
//解锁
reentrantLock.unlock();
}
}
System.out.println(Thread.currentThread().getName() + "卖了一张票,还剩" + (--ticket));
}
/**
* 主线程
*/
public static void main(String[] args) {
//创建子线程对象并启用
ThreadLock conflict = new ThreadLock();
Thread thread1 = new Thread(conflict, "售票员1");
Thread thread2 = new Thread(conflict, "售票员2");
Thread thread3 = new Thread(conflict, "售票员3");
thread1.start();
thread2.start();
thread3.start();
}
}
ThreadPool
在Java中线程的创建与销毁需要频繁的系统调度,会耗费很大的资源,频繁的创建于销毁线程会影响系统性能
使用线程池的好处
代码示例
public class ThreadPool implements Runnable {//线程池示例
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
//创建一个线程池服务
ExecutorService executorService = Executors.newFixedThreadPool(10);
//给池中添加线程
ThreadPool threadPool = new ThreadPool();
executorService.execute(new Thread(threadPool));
executorService.execute(new Thread(threadPool));
executorService.execute(new Thread(threadPool));
executorService.execute(new Thread(threadPool));
executorService.execute(new Thread(threadPool));
executorService.execute(new Thread(threadPool));
executorService.shutdown();//关闭线程池
}
}
public class Product {//产品类
private String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Producer implements Runnable {//生产者或厨师
private BlockingQueue<Product> blockingQueue;//成员变量并发集合,由构造方法提供初始化
public Producer(BlockingQueue<Product> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
int i = 0;
while (true) {
i = i % 10 + 1;
try {
Product product = new Product("产品" + i);
blockingQueue.put(product);
System.out.println(Thread.currentThread().getName() + "生产了----->" + product.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Consumer implements Runnable {//消费者类
private BlockingQueue<Product> blockingQueue;//成员变量并发集合,由构造方法进行初始化
public Consumer(BlockingQueue<Product> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
int i = 0;
while (i++ < 3) {
Product product;
try {
product = blockingQueue.take();
System.out.println(Thread.currentThread().getName() + "消费了" + product.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestClass {//测试类
public static void main(String[] args) {
//创建有并发控制的并发集合,用来存储产品
BlockingQueue<Product> blockingQueue = new ArrayBlockingQueue<>(10);
//创建生产者线程
Thread thread = new Thread(new Producer(blockingQueue), "生产者1");
//创建消费者线程
Consumer consumer = new Consumer(blockingQueue);
Thread thread1 = new Thread(consumer, "消费者1");
Thread thread2 = new Thread(consumer, "消费者2");
Thread thread3 = new Thread(consumer, "消费者3");
//运行线程
thread.start();
thread1.start();
thread2.start();
thread3.start();
}
}
public class MyStaticProxy {//静态代理示例
public static void main(String[] args) {
//创建Me对象,并将Me对象委托给WeddingCompany,调用WeddingCompany对象的结婚方法
Me me = new Me();
WeddingCompany weddingCompany = new WeddingCompany(me);
weddingCompany.marryMethod();
}
}
/**
* 结婚接口
*/
interface Marry {
void marryMethod();
}
/**
* 要结婚的人
*/
class Me implements Marry {
@Override
public void marryMethod() {
System.out.println("我要结婚了");
}
}
/**
* 婚庆公司
*/
class WeddingCompany implements Marry {
private Marry marry;
@Override
public void marryMethod() {
before();
this.marry.marryMethod();
after();
}
public WeddingCompany(Marry marry) {
this.marry = marry;
}
private void before() {
System.out.println("婚庆公司布置婚礼现场,准备流程");
}
private void after() {
System.out.println("付钱回家,婚庆公司收拾结尾");
}
}
public class LambdaExpressions {
/**
* 2. 使用静态成员内部类的形式实现接口
*/
static class Love2 implements InLove {
@Override
public void lambda() {
System.out.println("2. 使用静态成员内部类的形式实现接口");
}
}
public static void main(String[] args) {
/**
* 1. 使用外部类的方式实现接口
*/
new Love1().lambda();
/**
*2. 使用静态成员内部类的形式实现接口
*/
new Love2().lambda();
/**
* 3. 使用局部内部类的形式实现接口
*/
class Love3 implements InLove {
@Override
public void lambda() {
System.out.println("3. 使用局部内部类的形式实现接口");
}
}
new Love3().lambda();
/**
* 4. 使用匿名内部类的形式实现接口
*/
new InLove() {
@Override
public void lambda() {
System.out.println("4. 使用匿名内部类的形式实现接口");
}
}.lambda();
/**
* 5. 使用Lambda表达式的形式实现接口
*/
InLove love5 = () -> {
System.out.println("5. 使用Lambda表达式的形式实现接口");
};
love5.lambda();
}
}
/**
* 接口
*/
interface InLove {
public void lambda();
}
/**
* 1. 使用外部类的形式实现接口
*/
class Love1 implements InLove {
@Override
public void lambda() {
System.out.println("1. 使用外部类的形式实现接口");
}
}
ArrayList集合:线程不安全的
public class ArrayLists {//ArrayList的线程不安全
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
/**
* 使用Lambda表达式将Runnable接口的子类实现传递给Thread类的构造方法
*/
new Thread(() -> {
/**
* 为了解决ArrayList集合的线程不安全问题,可以将插入元素的语句锁起来,这样子就不会有并发发生
*/
synchronized ("") {
arrayList.add(Thread.currentThread().getName());
}
}).start();
}
/**
* 因为主线程也是一个线程,可能主线程的集合长度都打印出来了,子线程的元素还没有添加完,
* 所以先让主线程休眠1秒,等子线程的元素添加完之后再打印元素个数
*/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(arrayList.size());
}
for (String s : arrayList) {//打印集合中的元素
System.out.println(s);
}
}
}
CopyOnWriteArray集合:线程安全的
public class CopyOnArrayLists {//多线程安全的集合
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();//创建多线程安全的集合
for (int i = 0; i < 100; i++) {//创建100个子线程,使用Lambda表达式的形式
new Thread(() -> {//Lambda表达式,类似于匿名内部类,在参数为抽象类或接口时使用,不用创建接口或抽象类的子类对象
list.add(Thread.currentThread().getName());//将子线程的名称添加到集合中
}).start();//开启线程
}
try {
Thread.sleep(100);//让主线程睡眠100毫秒,先让其他的子线程执行
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(list.size());
}
for (String s : list) {//遍历打印集合的内容
System.out.println(s);
}
}
}
完…