Java badge accumulation (multi-thread)

xiaoxiao2021-03-06  140

Today, learn the multi-threaded multithread of Java, read half of the book, the card, write a procedure, misunderstanding, but also consolidate the knowledge, the procedure is as follows:

//Self.javaimport javax.swing. *; Import java.awt.event. *; Import java.awt. *;

public class Sell extends JFrame implements ActionListener {JTextArea taOut; JButton btnStart; JButton btnStop; JTextField jtf; Producethread produce; Customthread customer; Things thing; JPanel p1; JScrollPane p;

public Sell () {p1 = new JPanel (); taOut = new JTextArea (20,30); p1.add (taOut); p = new JScrollPane (p1); p.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); p.setVerticalScrollBarPolicy ( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); btnStart = new JButton ( "Start"); btnStart.addActionListener (this); btnStop = new JButton ( "Stop"); btnStop.addActionListener (this); jtf = new JTextField (6); getContentPane () .add (p); getContentPane (). add (jtf); getContentPane (). add (btnStart); getContentPane (). add (btnstop); getContentPane (). setLayout (new flowLayout ()); taOut); setTitle ( "Sell Test"); setBounds (100,0,370,460); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setVisible (true);} public void actionPerformed (ActionEvent ae) {if (ae.getActionCommand (). equals ( " START ")) {product = new productThread (Thing); Customer = New CustomThread (Thing); Customer.SetSpeed ​​(JTF.GetText ()); product.start (); customer.start ();} if (ae.ge) TACTIONCOMMAND (). Equals ("stop")) {product.stop (); customer.stop ();}} public static void main (String [] args) {Sell SE = New Sell ();}}

class Producethread extends Thread {Things th; public Producethread (Things thing) {th = thing;} public void run () {while (true) {th.addProduce (); try {sleep (500);} catch (InterruptedException ie) {System.err.Println ("Err:" IE.TOString ());}}}} CLASS CUSTOMTHREAD EXTENDS Thread {Things Th; Int Sh; Public CustomThread (Things Thing) {TH = Thing;} Public Void Run ) {While {th.sellProduce (); try {sleep ((int));} catch (interruptedException IE) {system.err.println ("Err:" IE .tostring ());}}} public void setspeed (string s) {sh = integer.parseint (s) * 2;}}}}}}}}}}} class things {jtextarea out; int total; private final static int max = 50; public strs (Public things) Jtextarea taout) {OUT = TAOUT; TOTAL = 10;} public synchronized void addproduce () {if (Total 0) {Total ---; out.append ("sale One product / N stock: " Total " / n ");} else {out.append (" out of stock, waiting in production! / N "); Wait ();}} catch (interruptedException IE) {system. Err.Println ("Err:" IE.tostring ());} notify ();}}

The program can operate correctly, operate in normal mode, but when continuous twice or on START, the problem is coming, in the actionPerformed method, processing START is re-instanting two thread objects and running, However, if the two objects are in operation, the old two objects will become uncontrolled, and the modification method can increase the call () method before re-instantification. Or add a variable to determine if the two objects are running, and the re-instantiation is not performed. In the test, it was found that the STOP () method was called, and it was not possible to re-start with the start () method. (There is no clear writing). Continuous calling the same thread object twice the start () method will be wrong, the stop () method will not.

转载请注明原文地址:https://www.9cbs.com/read-127415.html

New Post(0)