| | |
why is this simple code using 100% CPU?
![]() |
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
I just inctance 5 JFrames with string drawed and its using 100% ...???
that add(panel); make the diference in CPU 5 or 100%
I marked it in the comment in the code
here's the code:
that add(panel); make the diference in CPU 5 or 100%
I marked it in the comment in the code
here's the code:
Java Syntax (Toggle Plain Text)
//import java.awt.event.WindowEvent; //import java.awt.event.WindowListener; import java.awt.*; //import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.*; import javax.swing.*; //import javax.swing.JPanel; import java.util.Timer; import java.util.TimerTask; public class WinLsnrExp extends JFrame { private boolean a; public WinLsnrExp() { setTitle("Suncica2222"); setSize(150, 150); KlasaPanel panel = new KlasaPanel(); //Container panel = getContentPane(); panel.setBackground(Color.green); add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!! //repaint(); panel.repaint(); //panel1.setBackground(Color.red); } } class BrojiKlikove { public static int a=0; public BrojiKlikove() { a++; System.out.println(a); for (int i=0;i<a-4;i++){ CekajSekundi y= new CekajSekundi(0); KlasaKojaSlusa x=new KlasaKojaSlusa(); } } } class KlasaKojaSlusa extends JFrame { private static boolean b;//nicemu ne sluzi public KlasaKojaSlusa(String s){} public KlasaKojaSlusa() { WinLsnrExp wle = new WinLsnrExp(); //wle.setTitle("Suncica2222 "); //wle.setSize(150, 150); //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750)); wle.setVisible(true); wle.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.out.println("Window Closing"); //this.b=true; // a cannot be resolved or is not a field BrojiKlikove x=new BrojiKlikove(); } }); }// konstrukt } class KlasaPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); //g.Font(50); Font font = new Font("Serif", Font.PLAIN, 100); setFont(font); //g.setColor(Color.red); g.drawString("xD",10, 90); } }
Last edited by suncica2222; Jun 13th, 2009 at 3:16 pm.
Would be helpful if you can post also CekajSekundi class to see whole behaviour of your program.
Edit: Never mind above class I commented out the line where it is used. Can you post class with main method?
Edit: Never mind above class I commented out the line where it is used. Can you post class with main method?
Last edited by peter_budo; Jun 13th, 2009 at 3:31 pm.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
offcourse
those 2 classes are very simple
those 2 classes are very simple
Java Syntax (Toggle Plain Text)
public class KlasaZaMain { public static void main(String[] args) throws Exception { for (int i=0;i<5;i++){ KlasaKojaSlusa x=new KlasaKojaSlusa(); CekajSekundi y= new CekajSekundi(0); //x.praviProzor(); //y=new KlasaKojaSlusa("xaxaxa"); //KlasaKojaSlusa.brojiKlikove(); } /*while(true){ Thread.sleep(500); }*/ //if (KlasaKojaSlusa x==null) System.out.println(BrojiKlikove.a + "izbrojao sam"); } }//klasa
Java Syntax (Toggle Plain Text)
public class CekajSekundi { public CekajSekundi(int sekunde) { try { //wait(sekunde*1000); Thread.sleep(sekunde*1000); } catch (Exception e) {} } }
I believe the whole situation is created because of this random idea of handling Threads. Maybe somebody else can help you. However you may try to explain what you trying to achieve...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
I know what it does I actually took time to try it out. I do not understand purpose behind that program of what you trying to achieve. Are you trying to learn about threads? What you been trying to achieve when you code it?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
the program should be a lamer application that busts balls of user with popping windows from time to time
the only input parameter that we have is the listener on the red X closing button
the idea is that more is user trying to close every one of them,the more it pops out
so I have counter of clicks,I need also a timer so I could divide it to find out the speed of clicking and then calculate how much new windows it's going to pop out
so I need one class in which I'll be able to control all this and add some amount of randomness
and off course program should not be running CPU more than 3%
class just for calling everything in main()
class which constructor make pause of int seconds in running program
the only input parameter that we have is the listener on the red X closing button
the idea is that more is user trying to close every one of them,the more it pops out
so I have counter of clicks,I need also a timer so I could divide it to find out the speed of clicking and then calculate how much new windows it's going to pop out
so I need one class in which I'll be able to control all this and add some amount of randomness
and off course program should not be running CPU more than 3%
Java Syntax (Toggle Plain Text)
//import java.awt.event.WindowEvent; //import java.awt.event.WindowListener; import java.awt.*; //import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.*; import javax.swing.*; //import javax.swing.JPanel; import java.util.Timer; import java.util.TimerTask; //class just for frame and panel painting public class WinLsnrExp extends JFrame { private boolean a; public WinLsnrExp() { setTitle("Suncica2222"); setSize(150, 150); KlasaPanel panel = new KlasaPanel(); //Container panel = getContentPane(); panel.setBackground(Color.green); add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!! //repaint(); panel.repaint(); //panel1.setBackground(Color.red); } } //class that counts clicks on red X closing button of the window class BrojiKlikove { public static int a=0; public BrojiKlikove() { a++; System.out.println(a); for (int i=0;i<a-4;i++){ CekajSekundi y= new CekajSekundi(0); KlasaKojaSlusa x=new KlasaKojaSlusa(); } } } //class that makes the window frame and has a listener on red X //closing button class KlasaKojaSlusa extends JFrame { private static boolean b;//nicemu ne sluzi public KlasaKojaSlusa(String s){} public KlasaKojaSlusa() { WinLsnrExp wle = new WinLsnrExp(); //wle.setTitle("Suncica2222 "); //wle.setSize(150, 150); //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750)); wle.setVisible(true); wle.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.out.println("Window Closing"); //this.b=true; // a cannot be resolved or is not a field BrojiKlikove x=new BrojiKlikove(); } }); }// konstrukt } //class for drawing "xD" string on panel //I would also like to draw some more strings but it wont let me... class KlasaPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); //g.Font(50); Font font = new Font("Serif", Font.PLAIN, 100); setFont(font); //g.setColor(Color.red); g.drawString("xD",10, 90); } }
class just for calling everything in main()
Java Syntax (Toggle Plain Text)
public class KlasaZaMain { public static void main(String[] args) throws Exception { for (int i=0;i<5;i++){ KlasaKojaSlusa x=new KlasaKojaSlusa(); CekajSekundi y= new CekajSekundi(0); //x.praviProzor(); //y=new KlasaKojaSlusa("xaxaxa"); //KlasaKojaSlusa.brojiKlikove(); } /*while(true){ Thread.sleep(500); }*/ //if (KlasaKojaSlusa x==null) System.out.println(BrojiKlikove.a + "izbrojao sam"); } }//klasa
class which constructor make pause of int seconds in running program
Java Syntax (Toggle Plain Text)
public class CekajSekundi { public CekajSekundi(int sekunde) { try { //wait(sekunde*1000); Thread.sleep(sekunde*1000); } catch (Exception e) {} } }
I think I can shed some light on why people have stopped replying... People don't like that sort of application, so not many people are willing to help create one.
if I were to give a fast guess, although it's not necessarily true, I would say that it's probably not a good idea to make a thread sleep within a constructor.
if I were to give a fast guess, although it's not necessarily true, I would say that it's probably not a good idea to make a thread sleep within a constructor.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
![]() |
Similar Threads
- svchost.exe using 100% cpu! (Windows NT / 2000 / XP)
- svchost.exe 100% cpu usage pissing me off :( (Windows NT / 2000 / XP)
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP)
- Windows 2000 pro - svchost.exe using 100% cpu (Windows NT / 2000 / XP)
- svchost.exe takes 100% of cpu (Windows NT / 2000 / XP)
- Please Check My HJT Log - No internet- constant 100% CPU - HD constantly being read (Viruses, Spyware and other Nasties)
- DVD/CD-Rom drive using 100% CPU (Storage)
- super slow; 100% CPU usage - what's wrong? (Viruses, Spyware and other Nasties)
- Outlook Express 6 is frozen, and uses 100% CPU (Windows Software)
Other Threads in the Java Forum
- Previous Thread: Plus or minus
- Next Thread: Simple Chat Server
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






