目录
3.子线程循环3次,接着主线程循环5次,再子线程循环3次,主线程循环5次,如此反复,循环3次
4.两个线程,一个线程打印1~ 52,另一个线程打印A~Z,打印顺序是12A34B…5152Z
5.写10个线程,第一个线程从1加到10,第二个线程从11加20…第十个线程从91加到100,最后再把10个线程结果相加
1.两个线程交替打印1-100
package com.example.practice.Thread;
/**
* @author liuxiaobai
*/
public class WindowSaleTicket {
public static void main(String[] args) {
Window window = new Window();
new Thread(window).start();
new Thread(window).start();
}
}
class Window implements Runnable {
int count = 0;
@Override
public void run() {
while (true) {
synchronized (this) {
if (count >= 100) {
notify();
break;
}
count++;
System.out.println(Thread.currentThread().getName() + " " + count);
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
2.三个线程交替打印1-100
package com.example.practice.Thread;
class PrintNumber {
private int number = 1;
private int windowNumber = 1;
void firstThreadPrint() {
synchronized (this) {
while (windowNumber != 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ": " + number);
number++;
windowNumber = 2;
notifyAll();
}
}
void secondThreadPrint() {
synchronized (this) {
while (windowNumber != 2) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ": " + number);
number++;
windowNumber = 3;
notifyAll();
}
}
void thirdThreadPrint() {
synchronized (this) {
while (windowNumber != 3) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ": " + number);
number++;
windowNumber = 1;
notifyAll();
}
}
}
public class WindowSaleTicket {
public static void main(String[] args) {
PrintNumber printNumber = new PrintNumber();
new Thread(() -> {
for (int i = 0; i < 34; i++) {
printNumber.firstThreadPrint();
}
}).start();
new Thread(() -> {
for (int i = 0; i < 33; i++) {
printNumber.secondThreadPrint();
}
}).start();
new Thread(() -> {
for (int i = 0; i < 33; i++) {
printNumber.thirdThreadPrint();
}
}).start();
}
}
3.子线程循环3次,接着主线程循环5次,再子线程循环3次,主线程循环5次,如此反复,循环3次
package com.example.practice.Thread;
class Job {
private boolean executeFlag = true;
private int number = 1;
void sonThreadPrint() {
synchronized (this) {
while (executeFlag) {
if (number > 3) {
break;
}
notify();
for (int i = 1; i <= 3; i++) {
System.out.println("子线程输出" + i + "次");
}
try {
executeFlag = false;
number++;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
void monsterThreadPrint() {
synchronized (this) {
while (!executeFlag) {
notify();
for (int i = 1; i <= 5; i++) {
System.out.println("主线程输出" + i + "次");
}
try {
executeFlag = true;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class WindowSaleTicket {
public static void main(String[] args) {
Job job = new Job();
new Thread(() -> {
job.sonThreadPrint();
}).start();
try {
//休眠5毫秒,确保程序先执行子线程
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
job.monsterThreadPrint();
}
}
4.两个线程,一个线程打印1~ 52,另一个线程打印A~Z,打印顺序是12A34B…5152Z
package com.example.practice.Thread;
class Job {
private int count = 0;
void printNumber() {
for (int i = 0; i < 26; i++) {
synchronized (this) {
notify();
System.out.print(++count);
System.out.print(++count);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
void printChar() {
for (int i = 0; i < 26; i++) {
synchronized (this) {
notify();
System.out.print((char) (65 + i));
try {
if (i == 25) {
break;
}
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class WindowSaleTicket {
public static void main(String[] args) {
Job job = new Job();
new Thread(() -> job.printNumber()).start();
new Thread(() -> job.printChar()).start();
}
}
5.写10个线程,第一个线程从1加到10,第二个线程从11加20…第十个线程从91加到100,最后再把10个线程结果相加
package com.example.practice.Thread;
class Job implements Runnable {
private int count = 1;
int sum = 0;
@Override
public void run() {
synchronized (this) {
for (int i = count; i < count + 10; i++) {
sum += i;
}
count += 10;
}
}
}
/**
* @author liuxiaobai
*/
public class WindowSaleTicket {
public static void main(String[] args) {
Job job = new Job();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(job);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(job.sum);
}
}
6.三个窗口同时卖票
package com.example.practice.Thread;
class Ticket {
private int count = 100;
void saleTicket() {
while (true) {
synchronized (this) {
if (count <= 0) {
break;
}
System.out.println(Thread.currentThread().getName() + " 剩余票数" + --count);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Window implements Runnable {
private final Ticket ticket;
public Window(Ticket ticket) {
this.ticket = ticket;
}
@Override
public void run() {
ticket.saleTicket();
}
}
/**
* @author liuxiaobai
*/
public class WindowSaleTicket {
public static void main(String[] args) {
Window window = new Window(new Ticket());
new Thread(window).start();
new Thread(window).start();
new Thread(window).start();
}
}