开启两个线程A、B,打印1到10,线程A打印奇数(1、3、5、7、9),线程B打印偶数(2、4、6、8、10)。
package com.company.multi_thread.PrintOddEvenWN;
class TaskEvenOdd implements Runnable {
private int max;
private Printer print;
private boolean isEvenNumber;
public TaskEvenOdd(Printer print, int max, boolean isEvenNumber) {
this.max = max;
this.print = print;
this.isEvenNumber = isEvenNumber;
}
// standard constructors
@Override
public void run() {
int number = isEvenNumber ? 2 : 1;
while (number <= max) {
if (isEvenNumber) {
print.printEven(number);
} else {
print.printOdd(number);
}
number += 2;
}
}
static class Printer {
private volatile boolean isOdd;
synchronized void printEven(int number) {
while (!isOdd) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println(Thread.currentThread().getName() + ":" + number);
isOdd = false;
notify();
}
synchronized void printOdd(int number) {
while (isOdd) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println(Thread.currentThread().getName() + ":" + number);
isOdd = true;
notify();
}
}
public static void main(String... args) {
Printer print = new Printer();
Thread t1 = new Thread(new TaskEvenOdd(print, 10, false),"Odd");
Thread t2 = new Thread(new TaskEvenOdd(print, 10, true),"Even");
t1.start();
t2.start();
}
}
import java.util.concurrent.locks.LockSupport;
/**
* 开启两个线程A、B,打印1到10,线程A打印奇数(1、3、5、7、9),线程B打印偶数(2、4、6、8、10)。
*/
public class LockSupportDemo {
static Thread thread1;
static Thread thread2;
public static void main(String[] args) {
thread1 = new Thread(() -> {
for (int i = 1; i <= 9; i += 2) {
System.out.println(i);
LockSupport.unpark(thread2);
LockSupport.park();
}
});
thread2 = new Thread(() -> {
for (int i = 2; i <= 10; i = i + 2) {
LockSupport.park();
System.out.println(i);
LockSupport.unpark(thread1);
}
});
thread1.start();
thread2.start();
}
}
public class Demo{
public static void main(String[] args){
//打印奇数
new Thread(new Runnable(){
public void run(){
for(int i=1;i<=10;i++){
if(i%2!=0){
System.out.println(i);
}
}
}
},"A").start();
//打印偶数
new Thread(new Runnable(){
public void run(){
for(int i=1;i<=10;i++){
if(i%2==0){
System.out.println(i);
}
}
}
},"B").start();
}
}
/** * @author on 2021/2/23 */ public class test4 { public static void main(String[] args) { new Test5().start(); new Test1().start(); } } class Test5 extends Thread { @Override public void run() { for (int i=1;i<=10;i++){ if(i%2==1){ System.out.println(i); } } } } class Test1 extends Thread { @Override public void run() { for (int i=1;i<=10;i++){ if(i%2==0){ System.out.println(i); } } } }
package cn.wzb.come; import java.util.concurrent.locks.LockSupport; public class ComeTest { static Thread threadA; static Thread threadB; public static void main(String[] args) throws Exception { /** * 开启两个线程A、B,打印1到10,线程A打印奇数(1、3、5、7、9),线程B打印偶数(2、4、6、8、10)。 */ System.out.println("线程有序执行"); testA(); Thread.sleep(1000); System.out.println("线程交替执行"); testB(); } /** * 线程A先输出,然后线程B再输出 */ public static void testA(){ threadA = new Thread( new Runnable() { @Override public void run() { // 阻塞A线程 LockSupport.park(); for (int i = 1; i <= 10; i++) { if (i % 2 == 1) { System.out.println("ThreadA : " + Thread.currentThread().getName() + ",i : " + i); } } // 唤醒B线程 LockSupport.unpark(threadB); } } ); threadB = new Thread( new Runnable() { @Override public void run() { // 唤醒A线程 LockSupport.unpark(threadA); // 阻塞B线程 LockSupport.park(); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { // 锁定B线程 System.out.println("ThreadB : " + Thread.currentThread().getName() + ",i : " + i); } } } } ); threadA.start(); threadB.start(); } /** * 线程A、B交替输出 * @throws InterruptedException */ public static void testB() throws InterruptedException { threadA = new Thread( new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { if (i % 2 == 1) { System.out.println("ThreadA : " + Thread.currentThread().getName() + ",i : " + i); // 释放B线程 LockSupport.unpark(threadB); // System.out.println("释放B线程"); // 锁定A线程 // System.out.println("锁定A线程"); LockSupport.park(); } } } } ); threadB = new Thread( new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { // 锁定B线程 // System.out.println("锁定B线程"); LockSupport.park(); System.out.println("ThreadB : " + Thread.currentThread().getName() + ",i : " + i); // 释放A线程 // System.out.println("释放A线程"); LockSupport.unpark(threadA); } } } } ); threadA.start(); threadB.start(); } }
@Data static class Test implements Runnable { private static Integer maxNum; private Integer startNum; private String name; public Test(Integer startNum, String name) { this.startNum = startNum; this.name = name; } @Override public void run() { while (startNum <= maxNum) { System.out.println(name + ":" + startNum); startNum += 2; } } } public static void main(String[] args) { Test.maxNum = 10; new Thread(new Test(1, "a")).start(); new Thread(new Test(2, "b")).start(); }
有盆友说要按顺序,加了个顺序
@Data static class Test implements Runnable { private static Integer maxNum; private static Integer startNum; private Boolean isOdd; private String name; public Test(String name, Boolean isOdd) { this.name = name; this.isOdd = isOdd; } @Override public void run() { while (startNum <= maxNum) { if (currentIsOdd(startNum).equals(isOdd)) { System.out.println(name + ":" + startNum); startNum++; try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); break; } } } } public Boolean currentIsOdd(Integer num) { return num % 2 == 1; } } public static void main(String[] args) { Test.startNum = 1; Test.maxNum = 10; new Thread(new Test("a", true)).start(); new Thread(new Test("b", false)).start(); }
class Main implements Runnable{
volatile int i = 1;
@Override
public void run() {
synchronized (this){
while (true){
notify();
if(i<=10){
System.out.println(i);
i++;
}else break;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Main task = new Main();
new Thread(task).start();
new Thread(task).start();
}
}