1. Use the multi-threaded class to achieve 20 even and 20 odd random outputs.
//第一种创建线程的方法:继承Thread
public class NumberThread extends Thread{
int value;
public NumberThread(int value){
this.value = value;
}
public void run(){
//输出20个偶数或者奇数
for(int i = 0; i < 20; i++){
System.out.println(getName()+ "-----" + value);
value += 2;
}
}
}
public class FirstThread {
public static void main(String[] args){
NumberThread even = new NumberThread(0);
NumberThread odd = new NumberThread(1);
even.setName("偶数线程");
odd.setName("奇数线程");
even.start();
odd.start();
System.out.println("main线程结束");
}
}
2. Using multi-threaded programming to realize the problem of selling tickets in multiple windows:
(1) 10 tickets for each of the three windows are sold at the same time;
public class TicketThread extends Thread{
int count = 10;
public void run() {
while(count > 0){
count--;
System.out.println(getName() + "卖了1张票,还剩" + count + "张");
}
}
}
public class TicketTest {
public static void main(String[] args) {
TicketThread win1 = new TicketThread();
win1.setName("win1");
TicketThread win2 = new TicketThread();
win2.setName("win2");
TicketThread win3 = new TicketThread();
win1.setName("win3");
win1.start();
win2.start();
win3.start();
}
}
(2) The three windows sell 10 tickets together.
public class TicketRunnable implements Runnable{
int count = 10;
public void run() {
while(count > 0){
count--;
System.out.println(Thread.currentThread().getName() + "卖了1张票,还剩" + count + "张");
}
}
}
public class TicketTest {
public static void main(String[] args) {
TicketRunnable r4 = new TicketRunnable();
Thread win4 = new Thread(r4, "窗口4");
Thread win5 = new Thread(r4, "窗口5");
Thread win6 = new Thread(r4, "窗口6");
win4.start();
win5.start();
win6.start();
}
}
3. Write a multi-threaded program, implement producer and consumer threads, and realize thread synchronization:
(1) The producer thread generates 20 numbers, and the consumer thread outputs the 20 numbers generated by the producer thread.
//缓冲区:拥有一个值,可以设置可以获得
public class Buffer {
int value;
public int getValue() { //消费者,得到值
System.out.println(" 取得值" + value);
return value;
}
public void setValue(int value) { //生产者:设置值
this.value = value;
System.out.println("设置值" + value);
}
}
//生产者线程
public class SetThread extends Thread{
Buffer buffer;//缓冲区
public SetThread(Buffer buffer) {
super();
this.buffer = buffer;
}
public void run() {
for(int i = 0; i < 30; i++){
buffer.setValue(i);
}
}
}
//消费者线程
public class GetThread extends Thread{
Buffer buffer;//缓冲区
public GetThread(Buffer buffer) {
super();
this.buffer = buffer;
}
public void run() {
for(int i = 0; i < 30; i++){
buffer.getValue();
}
}
}
public class BufferTest {
public static void main(String[] args) {
Buffer buf = new Buffer();
SetThread setThread = new SetThread(buf);
GetThread getThread = new GetThread(buf);
setThread.start();
getThread.start();
}
}
(2) Use the synchronization and coordination mechanism of threads to achieve such an effect: generate a number and take out a number.
//缓冲区:拥有一个值,可以设置可以获得
public class Buffer {
int value;
boolean flag =false;//通信的标志。有没有值可取的标志
public synchronized int getValue() { //加上一把锁
if(!flag){//没有值
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(" 取得值" + value);
flag = false;
notify();//将等待这个资源的其他进程唤醒
return value;
}
public synchronized void setValue(int value) { //生产者:设置值
if(flag){//有数值可取
try {
wait();//Object类的一个方法
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//flag为false:没有数值可取,所以要进行设置
this.value = value;
System.out.println("设置值" + value);
flag = true;
notify();//唤醒消费者线程
}
}