1,编写多线程应用程序,模拟多个人通过一个山洞的模拟。
这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒,随机生成10个人,
同时准备过此山洞,显示一下每次通过山洞的人的名字。
public class Test1 {
public static void main(String[] args) {
new MyThread("张三").start();
new MyThread("李四").start();
new MyThread("王五").start();
new MyThread("赵六").start();
new MyThread("周期").start();
new MyThread("冯八").start();
new MyThread("小明").start();
new MyThread("小红").start();
new MyThread("丽华").start();
new MyThread("迭代").start();
}
}
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
public void run() {
synchronized (MyThread.class) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + "已经通过山洞");
}
}
}
2,定义一个类实现Runnable接口,卖票100张,四个线程,输出格式为“窗口1,还剩10张票”,指定窗口3卖第10张票。
public class Test2 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Thread t1 = new Thread(ticket, "窗口1");
Thread t2 = new Thread(ticket, "窗口2");
Thread t3 = new Thread(ticket, "窗口3");
Thread t4 = new Thread(ticket, "窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable {
private int ticket = 100;
@Override
public void run() {
while (true) {
synchronized (this) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//让窗口3卖第十张票
if (ticket == 10) {
if (!Thread.currentThread().getName().equals("窗口3")) {
continue;
}
}
System.out.println(Thread.currentThread().getName() + ",还剩" + (--ticket) + "张票");
} else {
break;
}
}
}
}
}
3,声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),给数组中添加数据,每一个线程为数组添加3个数据即可
public class Test3 {
public static void main(String[] args) {
//创建我的多线程对象
MyThread mt = new MyThread();
//开启线程
new Thread(mt, "线程1").start();
new Thread(mt, "线程2").start();
}
public static class MyThread implements Runnable {
int[] arr = new int[6];
Random r = new Random();
int count = 0;
@Override
public void run() {
//计数的个数小于数组的长度
while (count < arr.length) {
synchronized (r) {
//将获得的随机数赋值给数组
arr[count] = r.nextInt(100) + 1;
int num = arr[count];
count++;
System.out.println(Thread.currentThread().getName() + "想数组中添加了" + num);
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
4,同时开启两个线程,共同输出1-100之间的所有数字,并且将输出奇数的线程名称打印出来
public class Test4 {
static Object object = new Object();
static int count = 1;
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
//创建线程
new Thread(new Runnable() {
@Override
public void run() {
while (count < 100) {
synchronized (object) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count % 2 == 1) {
System.out.println(count + ":奇数,输出线程:" + Thread.currentThread().getName());
} else {
System.out.println(count/*+":"+Thread.currentThread().getName()*/);
}
count++;
}
}
}
}).start();
}
}
}
5,启动3个线程打印递增的数字,线程1先打印1,2,3,4,5,然后线程2打印6,7,8,9,10,然后线程3打印11,12,13,14,15.
接着再由线程1打印16,17,18,19,20…以此类推,直到打印到75为止.程序的输出结果格式如下:
线程1:1
线程1:2
线程1:3
线程1:4
线程1:5
线程2:6
线程2:7
线程2:8
线程2:9
线程2:10
线程3:11
线程3:12
线程3:13
线程3:14
线程3:15
...
线程3:71
线程3:72
线程3:73
线程3:74
线程3:75
public class Test5 {
public static void main(String[] args) {
PrintNum printNum=new PrintNum();
//线程1
new Thread(new Runnable() {
@Override
public void run() {
while (printNum.num<=65) {
printNum.fun1();
}
}
}, "线程1").start();
//线程2
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printNum.num<=70) {
printNum.fun2();
}
}
}, "线程2").start();
//线程3
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printNum.num<=75) {
printNum.fun3();
}
}
}, "线程3").start();
}
}
class PrintNum {
public int num = 1;//从1开始打印
public int isPrint = 1;//标识,1,2,3中标识分别代表三种状态
//标识1
public synchronized void fun1() {
while (isPrint != 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printXunHuan();
isPrint = 2;
notifyAll();
}
//标识2
public synchronized void fun2() {
while (isPrint!=2) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
printXunHuan();
isPrint=3;
notifyAll();
}
//标识3
public synchronized void fun3() {
while (isPrint!=3) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=1;
notifyAll();
}
public void printXunHuan() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + num++);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}