我们学校今年刚把java做为选修中的必修课,也就是说java是一门选修课,但是每个学生都必须学。(真不知道学院的老师是什么逻辑)这道题是课程设计上的一道实验题,可能我上学期的操作系统学的不够深,对线程没完全弄懂。
再问个低级的问题。例:
    class Oval{
        //变量定义
        void method1{}
        void method2{}
    }
问题:方法1和方法2定义为临界区时,Oval类的对象的变量是不是也属于临界区了???如果methed1的线程等待,其他线程能访问此变量吗???

解决方案 »

  1.   

    就这个问题没必要用多线程弄得这么复杂,用timer就可以
      

  2.   

    如此简单的功能,一个线程就够了。
    把主线程的函数写成这样:  public void run()
      {
        while(true)
        {
          if(radius < 150)
          {
              this.radius++;
          }
          else
          {
              this.radius = 0;
              color = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
          }
          this.repaint();
          try
          {
            Thread.currentThread().sleep(20);
          }
          catch(InterruptedException  x)
          {     
          }
        }    
      }
    绘画函数如下:public void paint( Graphics g )
    {
      g.setColor(this.color);
      g.fillOval(100,100,this.radius,this.radius);
    }就实现了。
      

  3.   

    完整代码如下:package ex041122;import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.lang.*;
    import java.lang.Thread;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;public class myApplet extends Applet implements Runnable{
      private boolean isStandalone = false;  private int radius = 0;
      private Color color = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
      private Thread drawT;
      JButton jButton1 = new JButton();
      XYLayout xYLayout1 = new XYLayout();
      
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }
      
      public void run()
      {
        while(true)
        {
          if(radius < 150)
          {
              this.radius++;
          }
          else
          {
              this.radius = 0;
              color = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
          }
          this.repaint();
          try
          {
            Thread.currentThread().sleep(20);
          }
          catch(InterruptedException  x)
          {     
          }
        }    
      }  //Construct the applet
      public myApplet() {
      }  //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }  //Component initialization
      private void jbInit() throws Exception {
        jButton1.setText("jButton1");
        jButton1.addActionListener(new myApplet_jButton1_actionAdapter(this));
        this.setLayout(xYLayout1);
        this.add(jButton1, new XYConstraints(8, 6, -1, -1));
      }  //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }public void paint( Graphics g )
    {
      g.setColor(this.color);
      g.fillOval(100,100,this.radius,this.radius);
    }
      //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }  void jButton1_actionPerformed(ActionEvent e) {
        drawT = new Thread(this);
        drawT.start();
      }
    }class myApplet_jButton1_actionAdapter implements java.awt.event.ActionListener {
      myApplet adaptee;  myApplet_jButton1_actionAdapter(myApplet adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }
      

  4.   

    回复mighthline:java里的Color类的三色取值范围是不是0-255???
    你的程序对我有点启发,我已经完成了此程序,下次发个帖子请你看看有什么可改的地方.
      

  5.   

    有兴趣的话再出道题:
        点击Applet上的按钮放大或者缩小上面的一串字符串,字符串的位置始终在Applet的中间.