package 任务十__多线程;
/** * @author ${范涛之} * @Description * @create 2021-11-22 21:20 */
public class Test1 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁1") {
System.out.println("t1 start");
try {
// t1 释放锁
"锁1".wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t1 end");
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁2") {
System.out.println("t2 start");
try {
// 通知 t1 进入等待队列
"锁2".wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t2 end");
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁3") {
System.out.println("t3 start");
try {
// 通知 t3 进入等待队列
"锁3".notify();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t3 end");
}
}
});
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁4") {
System.out.println("t4 start");
try {
// 通知 t3 进入等待队列
"锁4".notify();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t4 end");
}
}
});
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package 任务十__多线程;
/** * @author ${范涛之} * @Description * @create 2021-11-22 21:20 */
public class Test1 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁1") {
System.out.println("t1 start");
try {
// t1 释放锁
"锁1".wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t1 end");
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁2") {
System.out.println("t2 start");
try {
// 通知 t1 进入等待队列
"锁2".wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t2 end");
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁1") {
System.out.println("t3 start");
try {
// 通知 t3 进入等待队列
"锁1".notify();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t3 end");
}
}
});
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
synchronized ("锁2") {
System.out.println("t4 start");
try {
// 通知 t3 进入等待队列
"锁2".notify();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("t4 end");
}
}
});
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package 任务十__多线程;
/** * @author ${范涛之} * @Description * @create 2021-11-24 16:44 */
public class Tick {
public static void main(String[] args) {
Window w1 = new Window("柜台一");
Window w2 = new Window("柜台二");
Window w3 = new Window("柜台三");
w1.start();
w2.start();
w3.start();
}
}
class Window extends Thread{
private static int number = 100;
public Window(String name){
super(name);
}
@Override
public void run() {
while (number>0){
number--;
System.out.println(getName()+"兑换成功一张票"+"票号为:"+number);
}
}
}
package 任务十__多线程.售票;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** * @author ${范涛之} * @Description * @create 2021-11-24 20:25 */
public class Tick1 {
public static void main(String[] args) {
List list = new ArrayList();
for (int i = 1; i <= 100; i++){
list.add(i);
}
Iterator iterator = list.iterator();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (iterator.hasNext()) {
for (int i = 0; i < list.size(); i++) {
synchronized ("lock") {
String threadname = Thread.currentThread().getName();
System.out.println(threadname + "柜台正在兑换" + list.get(i) + "台iPhone");
list.remove(i); // 删除已经遍历的元素
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (iterator.hasNext()) {
for (int i = 0; i < list.size(); i++) {
synchronized ("lock") {
String threadname = Thread.currentThread().getName();
System.out.println(threadname + "柜台正在兑换" + list.get(i) + "台iPhone");
list.remove(i);
}
}
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
while (iterator.hasNext()) {
for (int i = 0; i < list.size(); i++) {
synchronized ("lock") {
String threadname = Thread.currentThread().getName();
System.out.println(threadname + "柜台正在兑换" + list.get(i) + "台iPhone");
list.remove(i);
}
}
}
}
});
t1.start();
t2.start();
t3.start();
}
}
package 任务十__多线程.售票;
/** * @author ${范涛之} * @Description * @create 2021-11-24 21:43 */
public class Ticket implements Runnable {
private int num1 = 0; //出票数
private int num2 = 100; //剩余票数
@Override
public void run() {
while (true) {
synchronized (this) {
if (num2 <= 0) {
break;
}
num1++;
num2--;
System.out.println("显示出票信息" + Thread.currentThread().getName() + "抢到第" + num1 + "张票,剩余" + num2 + "张票");
}
}
}
static class Test {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Thread g1 = new Thread(ticket,"柜台一");
Thread g2 = new Thread(ticket,"柜台二");
Thread g3 = new Thread(ticket,"柜台三");
g1.start();
g2.start();
g3.start();
}
}
}
package 任务十__多线程.售票;
/** * @author ${范涛之} * @Description * @create 2021-11-24 23:43 */
class SaleThread implements Runnable {
/** * 使用静态成员变量作为100张票的保存变量,是一个共享资源。 */
private static int tickets = 20;
@Override
public void run() {
// 完成售票过程
while (true) {
/* 字符串可以作为锁对象,因为双引号包含的字符串不管在代码中如何运行,有且只有一个 */
synchronized (Thread.currentThread().getName()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "售出了" + tickets + "张票");
tickets--;
} else {
System.out.println(Thread.currentThread().getName() + "售罄!!!");
break;
}
}
}
}
}
public class Demo {
public static void main(String[] args) {
Thread t1 = new Thread(new SaleThread(), "售票人员1");
Thread t2 = new Thread(new SaleThread(), "售票人员2");
Thread t3 = new Thread(new SaleThread(), "售票人员3");
t1.start();
t2.start();
t3.start();
}
}
synchronized (Thread.currentThread().getName()) {}
synchronized{}
,但是这样写会导致synchronized (this) 会连续执行当前线程,所以会出现大片大片一样的代码
package 任务十__多线程.售票;
/** * @author ${范涛之} * @Description * @create 2021-11-24 23:41 */
public class Ticket2 extends Thread {
private int num1 = 0;
private int num2 = 100;
@Override
public void run() {
while (true) {
synchronized (Thread.currentThread().getName()){ ;
if (num2 <= 0) {
break;
}
num1++;
num2--;
System.out.println("显示出票信息" + Thread.currentThread().getName() + "抢到第" + num1 + "张票,剩余" + num2 + "张票");
}
}
}
}
class Function{
public static void main(String[] args) {
Ticket2 ticket2 = new Ticket2();
Thread t1 = new Thread(ticket2,"柜台一");
Thread t2 = new Thread(ticket2,"柜台二");
Thread t3 = new Thread(ticket2,"柜台三");
t1.start();
t2.start();
t3.start();
}
}
package 任务十__多线程.售票;
import java.util.ArrayList;
import java.util.List;
public class TestDemo {
//定义售票线程类(也就是窗口)
public static class Station extends Thread{
//构造方法给线程名字赋值
public Station(String name) {
super(name);
}
//票数要静态定义
static int tick=50;
//静态钥匙
static Object ob ="key"; //值是任意的
//重写run方法,实现售票操作
@Override
public void run() {
List<Integer> list = new ArrayList<>();
while (tick>0) {
synchronized("key") { //必须使用一个同步锁,进去的人会把钥匙拿在手上,出来后才能交出钥匙
if (tick>0) {
System.out.printf("%s卖出了第%d张票 \n",getName(),tick);
list.add(tick);
tick--;
}else {
System.out.printf("%s:票已售空 \n",getName());
}
}
try {
sleep((int)(Math.random()*3000)+1); //随机休息1-3000ms
}catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("%s 销售情况: %s \n",getName(),list.toString());
}
}
public static void main(String[] args) {
//实例化站台对象,并为每一个站台取名字(8个线程窗口一起卖5张票)
for (int i=1; i<=8; i++) {
String sName="窗口" + String.valueOf(i);
Station Station = new Station(sName);
Station.start();
}
}
}
package 任务十__多线程.售票;
import java.util.ArrayList;
import java.util.List;
public class TestDemo {
//定义售票线程类(也就是窗口)
public static class Station extends Thread{
//构造方法给线程名字赋值
public Station(String name) {
super(name);
}
//票数要静态定义
static int tick=50;
//静态钥匙
static Object ob ="key"; //值是任意的
//重写run方法,实现售票操作
@Override
public void run() {
List<Integer> list = new ArrayList<>();
while (tick>0) {
synchronized(this) { //必须使用一个同步锁,进去的人会把钥匙拿在手上,出来后才能交出钥匙
if (tick>0) {
System.out.printf("%s卖出了第%d张票 \n",getName(),tick);
list.add(tick);
tick--;
}else {
System.out.printf("%s:票已售空 \n",getName());
}
}
try {
sleep((int)(Math.random()*3000)+1); //随机休息1-3000ms
}catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("%s 销售情况: %s \n",getName(),list.toString());
}
}
public static void main(String[] args) {
//实例化站台对象,并为每一个站台取名字(8个线程窗口一起卖5张票)
for (int i=1; i<=8; i++) {
String sName="窗口" + String.valueOf(i);
Station Station = new Station(sName);
Station.start();
}
}
}
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
这里如果没有特殊需求要指定最大线程池数量的话,建议最大线程池数量=运行程序机器的cpu核心数,即
int cpuNubmer = Runtime.getRuntime().availableProcessors();
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(cpuNubmer);
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
System.out.println("提交时间: " + sdf.format(new Date()));
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("运行时间: " + sdf.format(new Date()));
}
}, 3, TimeUnit.SECONDS);//延迟3秒执行
scheduledThreadPool.shutdown();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
System.out.println("提交时间: " + sdf.format(new Date()));
scheduledThreadPool.schedule(() -> System.out.println("运行时间: " + sdf.format(new Date())), 3, TimeUnit.SECONDS);
scheduledThreadPool.shutdown();
package 任务十__多线程.周期线程池;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** * @author ${范涛之} * @Description * @create 2021-11-25 0:49 */
public class Test {
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
pool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("一分钟过去了");
}
}, 3, 1, TimeUnit.MINUTES);
}
}
改成秒:
package 任务十__多线程.周期线程池;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** * @author ${范涛之} * @Description * @create 2021-11-25 0:49 */
public class Test {
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
pool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("一分钟过去了");
}
}, 3, 1, TimeUnit.SECONDS);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121520228
内容来源于网络,如有侵权,请联系作者删除!
synchronized (Thread.currentThread().getName()) {}
synchronized{}
,但是这样写会导致synchronized (this) 会连续执行当前线程,所以会出现大片大片一样的代码