NetBeans--Java线程学习

本文介绍了一个使用Java实现的双线程模型,其中一个线程负责向栈中写入整数,另一个线程负责从栈中读取整数。通过synchronized关键字确保线程间的正确同步,并使用wait和notify方法来协调读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg20221204stackthreaddemo;

import java.util.Stack;

/**
 *
 * @author mikha
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Stack<Integer> stack=new Stack<>();
//        stack.setSize(3);
        //两个线程一个读一个写
        WriteThread writeThread=new WriteThread(stack);
        writeThread.setName("写线程");
        ReadThread readThread=new ReadThread(stack);
        readThread.setName("读线程");
        writeThread.start();
        readThread.start();
        
    }
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg20221204stackthreaddemo;

import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mikha
 */
public class ReadThread extends Thread{
    private Stack<Integer> stack;
    private int count;

    public ReadThread(Stack<Integer> stack) {
        this.stack = stack;
        count = 1;
    }

    @Override
    public void run() {
        System.out.println("写线程在运行");
        while(count<=100)
        {
            synchronized(stack)
            {
                if(stack.empty())
                {
                    try {
                        stack.wait();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ReadThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else//栈不空
                {
                    int data=stack.pop();
                    System.out.println(Thread.currentThread().getName()+"读出:"+data);//输出读出的是几
                    count++;
                    stack.notify();
                }
            }
        }
    }
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg20221204stackthreaddemo;

import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mikha
 */
public class WriteThread extends Thread{
    private Stack<Integer>stack;
    private int count;//线程中的个数

    public WriteThread(Stack<Integer> stack) {
        this.stack = stack;
        count=1;
    }

    @Override
    public void run() {
        System.out.println("写线程正在运行");
        while(count<=100)
        {
            synchronized(stack)//Syncronized 的目的是一次只允许一个线程进入由他修饰的代码段,从而允许他们进行自我保护
            {
                if(stack.size()==3)//栈满
                {
                    try {
                        stack.wait();//等待
                    } catch (InterruptedException ex) {
                        Logger.getLogger(WriteThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else
                {
                    stack.push(count);
                    System.out.println(Thread.currentThread().getName()+"写入:"+count);//输出写入的是几
                    count++;
                    stack.notify();//Object notify() 方法用于唤醒一个在此对象监视器上等待的线程。
                    
                }
            }
        }
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值