java 线程简单实例_Java一个简单的线程任务实例

[java]代码库package org.ph.javaee.training8;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**

* A simple thread task representation

* @author Pierre-Hugues Charbonneau

*

*/

public class Task {

// Object used for FLAT lock

private final Object sharedObject = new Object();

// ReentrantReadWriteLock used for WRITE & READ locks

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

/**

* Execution pattern #1

*/

public void executeTask1() {

// 1. Attempt to acquire a ReentrantReadWriteLock READ lock

lock.readLock().lock();

// Wait 2 seconds to simulate some work...

try { Thread.sleep(2000);}catch (Throwable any) {}

try {

// 2. Attempt to acquire a Flat lock...

synchronized (sharedObject) {}

}

// Remove the READ lock

finally {

lock.readLock().unlock();

}

System.out.println("executeTask1() :: Work Done!");

}

/**

* Execution pattern #2

*/

public void executeTask2() {

// 1. Attempt to acquire a Flat lock

synchronized (sharedObject) {

// Wait 2 seconds to simulate some work...

try { Thread.sleep(2000);}catch (Throwable any) {}

// 2. Attempt to acquire a WRITE lock

lock.writeLock().lock();

try {

// Do nothing

}

// Remove the WRITE lock

finally {

lock.writeLock().unlock();

}

}

System.out.println("executeTask2() :: Work Done!");

}

public ReentrantReadWriteLock getReentrantReadWriteLock() {

return lock;

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单Java线程队列实例,模拟队列实现排队叫号的过程: ```java import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class QueueExample { private Queue<Integer> queue = new LinkedList<>(); private Lock lock = new ReentrantLock(); private int currentTicketNumber = 0; public void addCustomer() { lock.lock(); try { queue.offer(currentTicketNumber++); System.out.println("新顾客取得号码:" + (currentTicketNumber - 1) + ",当前队列长度:" + queue.size()); } finally { lock.unlock(); } } public void serveCustomer() { lock.lock(); try { Integer ticketNumber = queue.poll(); if (ticketNumber == null) { System.out.println("队列为空,无法叫号"); } else { System.out.println("叫到号码:" + ticketNumber + ",当前队列长度:" + queue.size()); } } finally { lock.unlock(); } } public static void main(String[] args) { final QueueExample queueExample = new QueueExample(); // 开启 5 个线程模拟 5 个顾客取号 for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.addCustomer(); } }).start(); } // 开启 3 个线程模拟 3 个服务员叫号 for (int i = 0; i < 3; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.serveCustomer(); } }).start(); } } } ``` 这个例子中,我们定义了一个 `QueueExample` 类,其中有一个 `queue` 队列,用于存储所有顾客的票号。我们使用 `ReentrantLock` 类来实现线程安全。在 `addCustomer` 方法中,我们通过 `offer` 方法往队列中添加一个顾客的票号,并输出当前队列长度以及新顾客的票号。在 `serveCustomer` 方法中,我们通过 `poll` 方法从队列中获取一个顾客的票号,并输出当前队列长度以及叫到的顾客的票号。 在 `main` 方法中,我们开启了 5 个线程模拟 5 个顾客取号,以及 3 个线程模拟 3 个服务员叫号。你可以运行这个例子来看一下具体的输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值