1、利用多线程类实现20个偶数和20个奇数的随机输出。
//第一种创建线程的方法:继承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、利用多线程编程实现多个窗口卖票的问题:
(1)三个窗口同时各卖10张票;
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)三个窗口共同卖10张票。
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、编写多线程程序,实现生产者、消费者线程,并实现线程的同步:
(1)生产者线程产生20个数,消费者线程输出生产者线程产生的这20个数。
//缓冲区:拥有一个值,可以设置可以获得
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)使用线程的同步与协调机制使二者达到如此效果:产生一个数,取出一个数。
//缓冲区:拥有一个值,可以设置可以获得
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();//唤醒消费者线程
}
}