1.多线程模拟两个人跑步,每个线程代表⼀个⼈,可设置每个⼈跑步的速度,每跑完100⽶给出响应的信息,跑到终点之后给出响应的提示。
public class Test {
public static void main ( String[ ] args) {
RunRunnable r1 = new RunRunnable ( 2000 , 1 ) ;
RunRunnable r2 = new RunRunnable ( 500 , 1 ) ;
new Thread ( r1, "张三" ) . start ( ) ;
new Thread ( r2, "李四" ) . start ( ) ;
}
}
class RunRunnable implements Runnable {
private int time;
private int num;
public RunRunnable ( int time, int km) {
this . time = time;
this . num = km* 1000 / 100 ;
}
@Override
public void run ( ) {
while ( num > 0 ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "跑了100米!" ) ;
try {
Thread. currentThread ( ) . sleep ( this . time) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
num-- ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + "到达了终点!" ) ;
}
}
2.某楼盘摇号买房,分为普通号和VIP号,50个普通号,10个VIP号。VIP号的选房时间为普通号的2倍,开始普通号和VIP号并行叫号,叫到VIP号的概率比普通号更高,当普通号叫完第10号时,要求先让VIP号全部选完,再让普通号选房,用多线程模拟这个过程。
package com. southwind. thread;
public class Test {
public static void main ( String[ ] args) {
VIPThread vip = new VIPThread ( ) ;
vip. setPriority ( Thread. MAX_PRIORITY) ;
vip. start ( ) ;
NormalRunnable normal = new NormalRunnable ( ) ;
normal. setThread ( vip) ;
Thread thread2 = new Thread ( normal) ;
thread2. setPriority ( Thread. MIN_PRIORITY) ;
thread2. start ( ) ;
}
}
class VIPThread extends Thread {
@Override
public void run ( ) {
for ( int i = 1 ; i <= 10 ; i++ ) {
System. out. println ( "VIP" + i+ "正在选房" ) ;
try {
Thread. currentThread ( ) . sleep ( 2000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
class NormalRunnable implements Runnable {
private Thread thread;
public Thread getThread ( ) {
return thread;
}
public void setThread ( Thread thread) {
this . thread = thread;
}
@Override
public void run ( ) {
for ( int i = 1 ; i <= 50 ; i++ ) {
if ( i == 10 ) {
try {
thread. join ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
System. out. println ( "普通号" + i+ "正在选房" ) ;
try {
Thread. currentThread ( ) . sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
3.用多线程模拟网络购票,“游客”,“学生”,“代理”共同抢15张票。
public class Test {
public static void main ( String[ ] args) {
TicketRunnable ticketRunnable = new TicketRunnable ( ) ;
Thread t1 = new Thread ( ticketRunnable, "游客" ) ;
Thread t2 = new Thread ( ticketRunnable, "学生" ) ;
Thread t3 = new Thread ( ticketRunnable, "代理" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
class TicketRunnable implements Runnable {
public int count = 15 ;
public int num = 0 ;
@Override
public void run ( ) {
while ( count > 0 ) {
try {
Thread. currentThread ( ) . sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
synchronized ( TicketRunnable. class ) {
if ( count == 0 ) {
return ;
}
count-- ;
num++ ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "抢到了第" + num+ "张票,还剩" + count+ "张票" ) ;
}
}
}
}
package com. southwind. thread;
public class Test {
public static void main ( String[ ] args) {
TicketThread t1 = new TicketThread ( ) ;
TicketThread t2 = new TicketThread ( ) ;
TicketThread t3 = new TicketThread ( ) ;
t1. setName ( "游客" ) ;
t2. setName ( "学生" ) ;
t3. setName ( "代理" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
class TicketThread extends Thread {
public static int count = 15 ;
public static int num = 0 ;
@Override
public void run ( ) {
while ( num < 15 ) {
try {
Thread. currentThread ( ) . sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
synchronized ( TicketThread. class ) {
if ( num == 15 ) {
return ;
}
count-- ;
num++ ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "抢到了第" + num+ "张票,还剩" + count+ "张票" ) ;
}
}
}
}