Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: When i run the Program, the CPU usage become very high

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default When i run the Program, the CPU usage become very high

    Hi!
    I have written a program in Java, but when i run it the CPU usage become very high, if it run in a slow CPU computer, then the Computer become very very slow, why it is like this....Please tell me the reason
    Thank You!


  2. #2
    Junior Member Krumpir's Avatar
    Join Date
    Jul 2012
    Location
    Cape Town, South Africa
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: When i run the Program, the CPU usage become very high

    Your program is most likely stuck in an infinite loop. If you post the source code I we'll be able to point out the problem.
    Check out my site -> tssolutions.net16.net

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: When i run the Program, the CPU usage become very high

    Thank you for you reply!
    The code which has this problem is very long about 3000 lines in many classes, i will tell you the design which i have done, it like this: I have written one ATM System in three Languages, if i run only in one Language the problem is not very big, if run it in three Languages then the CPU usage goes to 100, the Computer become very slow, even it not working normally.... the code which i collected three languages in one frame i can post it:


    package com.ustb;

    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;

    import javax.swing.*;
    import javax.swing.border.Border;

    public class AllInOneFrame extends JFrame implements ActionListener, MouseListener {
    private static final long serialVersionUID = 1L;

    static JPanel cards;

    private JPanel panel1, panel2, panel3;

    private static JPanel panel4 = new JPanel();
    private JButton englishButton = new JButton("English");
    private JButton chinesehButton = new JButton("中文");
    private JButton persianhButton = new JButton("فارسی");

    private ImageIcon Up = new ImageIcon(getClass().getResource("image/Up.png"));
    private ImageIcon Up2 = new ImageIcon(getClass().getResource(
    "image/Up2.png"));
    private ImageIcon Do = new ImageIcon(getClass().getResource("image/Do.png"));
    private ImageIcon Do2 = new ImageIcon(getClass().getResource(
    "image/Do2.png"));
    private JLabel lableAdvertizeUp = new JLabel(Up);
    private JLabel lableAdvertizeDo = new JLabel(Do);
    private JLabel label = new JLabel();
    Border border = BorderFactory.createRaisedBevelBorder();

    ATMEnglish english = new ATMEnglish();
    ATMChinese chinese = new ATMChinese();
    ATMPersian persian = new ATMPersian();

    static boolean error;
    static boolean reSet = false;
    static ArrayList<String> errorIDList = new ArrayList<String>();

    Border border1 = BorderFactory.createLoweredBevelBorder();

    public AllInOneFrame() {
    panel1 = new JPanel(new FlowLayout());
    panel1.add(englishButton);
    panel1.add(chinesehButton);
    panel1.add(persianhButton);
    englishButton.setBackground(Color.decode("#800000" ));
    chinesehButton.setBackground(Color.decode("#800000 "));
    persianhButton.setBackground(Color.decode("#800000 "));
    englishButton.setBorder(border1);
    chinesehButton.setBorder(border1);
    persianhButton.setBorder(border1);
    MyFonts.setLabelFont(englishButton, Color.WHITE, 40);
    MyFonts.setLabelFont(chinesehButton, Color.WHITE, 40);
    MyFonts.setLabelFont(persianhButton, Color.WHITE, 40);
    MyFonts.setComponentFont(panel1, "#0000A0", Color.BLACK, null, 0);

    JLabel eng = new JLabel("Please Select Your Language", JLabel.CENTER);
    JLabel chn = new JLabel("请选择语言", JLabel.CENTER);
    JLabel per = new JLabel("لطفاً زبان خودرا انتخاب کنید", JLabel.CENTER);
    panel2 = new JPanel(new GridLayout(3, 0));
    panel2.add(eng);
    panel2.add(chn);
    panel2.add(per);
    MyFonts.setLabelFont(eng, Color.YELLOW, 50);
    MyFonts.setLabelFont(chn, Color.YELLOW, 50);
    MyFonts.setLabelFont(per, Color.YELLOW, 50);
    MyFonts.setComponentFont(panel2, "#0000A0", Color.BLACK, null, 0);

    panel3 = new JPanel(new BorderLayout());
    panel3.add(panel2, BorderLayout.NORTH);
    panel3.add(panel1, BorderLayout.CENTER);

    panel4 = new JPanel(new BorderLayout());
    panel4.add(panel3, BorderLayout.CENTER);

    cards = new JPanel();
    cards.setLayout(new CardLayout());
    cards.add("first", label);
    cards.add("center", panel4);
    cards.add("one", english.englishPanel());
    cards.add("two", chinese.chinesePanel());
    cards.add("three", persian.persianPanel());

    this.add(lableAdvertizeUp, BorderLayout.NORTH);
    this.add(cards, BorderLayout.CENTER);
    this.add(lableAdvertizeDo, BorderLayout.SOUTH);
    this.setSize(800, 600);
    this.setTitle("Auto Transfere Money");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setVisible(true);

    englishButton.addActionListener(this);
    chinesehButton.addActionListener(this);
    persianhButton.addActionListener(this);
    englishButton.addMouseListener(this);
    chinesehButton.addMouseListener(this);
    persianhButton.addMouseListener(this);
    label.addMouseListener(this);
    panel4.addMouseListener(this);

    new Thread(new MyAnimations.MyAnimation2(lableAdvertizeUp,
    lableAdvertizeDo, Up, Do, Up2, Do2)).start();
    MyAnimations anim2 = new MyAnimations();
    MyAnimations.MyAnimation3 animation2 = anim2.new MyAnimation3(label,"pic", 3);
    new Thread(animation2).start();
    new Thread(new WrongPasswordLocker()).start();
    }

    public static void main(String[] args) {
    new AllInOneFrame();

    }

    public class WrongPasswordLocker implements Runnable {

    public void run() {
    try {
    while (true) {
    for (int i = 1; i <= 60; i++) {
    Thread.sleep(1000);
    if(error) {
    i = 1;
    error = false;
    }
    }
    if(errorIDList.size() == 0)
    continue;
    else {
    errorIDList.clear();
    }
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    }

    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == englishButton) {
    english.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "one");
    }
    if(e.getSource() == chinesehButton) {
    chinese.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "two");
    }
    if(e.getSource() == persianhButton) {
    persian.startSeytem = true;
    reSet = true;
    ((CardLayout) cards.getLayout()).show(cards, "three");
    }
    }

    static void addListener() {
    //panel4.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {
    if(e.getSource() == englishButton) {
    englishButton.setBackground(Color.decode("#000000" ));
    }
    else if(e.getSource() == chinesehButton) {
    chinesehButton.setBackground(Color.decode("#000000 "));
    }
    else if(e.getSource() == persianhButton) {
    persianhButton.setBackground(Color.decode("#000000 "));
    }
    else if(e.getSource() == label)
    ((CardLayout) cards.getLayout()).show(cards, "center");
    }

    public void mouseExited(MouseEvent e) {
    if(e.getSource() == englishButton)
    englishButton.setBackground(Color.decode("#800000" ));
    else if(e.getSource() == chinesehButton)
    chinesehButton.setBackground(Color.decode("#800000 "));
    else if(e.getSource() == persianhButton)
    persianhButton.setBackground(Color.decode("#800000 "));
    else if(e.getSource() == panel4)
    ((CardLayout) cards.getLayout()).show(cards, "first");

    }

    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    }

Similar Threads

  1. HashMap : Simple Usage and Understanding
    By weakprogrammer in forum Collections and Generics
    Replies: 1
    Last Post: June 25th, 2011, 03:37 PM
  2. Usage of ClassLoader.setSigners() method
    By AlexDorogensky in forum Java Theory & Questions
    Replies: 1
    Last Post: January 13th, 2011, 10:37 AM
  3. How to get a System's total CPU usage percentage using a java program
    By userj2ee in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 01:28 AM
  4. Eclipse is always locked [High CPU & RAM usage]
    By talha06 in forum Java IDEs
    Replies: 4
    Last Post: March 16th, 2010, 10:07 AM
  5. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM