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 4 of 4

Thread: Problem with java program and CPU usage

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

    Default Problem with java program and CPU usage

    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.... why it is like this. can anyone help me? thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Problem with java program and CPU usage

    You're going to have to be a lot more specific. What languages are you using? Are you launching them all at the same time? How much processor time does each program require? What kind of machine are you using? What does each program do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Problem with java program and CPU usage

    Thank You for your reply!
    I wana answer to your question step by step. I write it in Java Language, yes i launch then all in the same time, every program will not require very much processor time, because if i run it in one language it is almost OK, working very normally, I don't what do you mean what kind of machine, but i can say that i am using DELL laptop i5 2.67GHz processor with 4GB RAM, and Java 1.7 JDK. each program just doing as an Automatic Transfer Monet machine is do, namely (Deposit, Withdraw, Transfer money, Balance in Query, Exit) i think it is not much complex, there is three classes for for each language, and three classes which every language shared like Database class which has the get Connection and so on method which is used by these three languages. and at the end one other class is used to collect these three languages to one, the class name is AllInOneFrame, the other classes is extended JPanel, and implements Key, Action, and Mouse Listeners, and the AllInOneFrame class is extended the JFrame, when i run this class the CPU usage dramatically goes to 100, and Computer become very slow.... I don't if there is any way to post the Project, because if i post the entire code it is very long, but i will the last AllInOneFrame class for more information. thank you


    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

    }

    }

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Problem with java program and CPU usage

    Duplicate of When i run the Program, the CPU usage become very high
    Read the forum rules.
    http://www.javaprogrammingforums.com...uncements.html
    Thread locked.

Similar Threads

  1. When i run the Program, the CPU usage become very high
    By Bahramudin in forum Member Introductions
    Replies: 2
    Last Post: July 21st, 2012, 04:37 AM
  2. Insertion Sort - memory usage
    By lovon in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 6th, 2011, 03:00 PM
  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, 12: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