⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 There is a shared object – int array[] between Write and Read. Write sets the array, Read gets the a
💻 JAVA
字号:
package cwq4;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class test extends JFrame {

  private String sorttimes[] =
      {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
  private int sorttimes_int[] =
      {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  private JComboBox combobox;
  private JLabel label;
  private JTextArea textarea;
  private FlowLayout layout;

  //set up GUI
  public test() {
    super("Write / Read & Sort a shared array problem:");
    layout = new FlowLayout(FlowLayout.LEFT,10,10);

    Container container = getContentPane();
    container.setLayout(layout);

    label = new JLabel("please select sort times from the combobox: ");
    combobox = new JComboBox(sorttimes);
    container.add(label);
    container.add(combobox);

    textarea = new JTextArea(20,30);
    textarea.setEnabled(false);
    textarea.setBackground(Color.black);//.setForeground(Color.black);

    //ArraySynchronized sharedArray = new ArraySynchronized();

    combobox.addItemListener(
      new ItemListener(){
        public void itemStateChanged(ItemEvent event){
          if(event.getStateChange()==ItemEvent.SELECTED)
          {
            ArraySynchronized sharedArray = new ArraySynchronized();
            sharedArray.settimes
                (sorttimes_int[combobox.getSelectedIndex()]);

            textarea.removeAll();
            textarea.setText("the output of this program is:\n\n");
            // create threads
            WriteArray producer =
                new WriteArray( sharedArray, sharedArray.gettimes(), textarea );
            ReadSortArray consumer =
                new ReadSortArray( sharedArray, sharedArray.gettimes(), textarea );
            // start threads
            producer.start();
            consumer.start();
          }
        }
      }
    );
    container.add(textarea);
    container.add(new JScrollPane(textarea));
    setSize(400,500);
    this.setResizable(false);
    setVisible(true);
  }

  public static void main(String[] args) {
    test application = new test();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -