why is this simple code using 100% CPU?

Reply

Join Date: Jun 2009
Posts: 29
Reputation: suncica2222 is an unknown quantity at this point 
Solved Threads: 0
suncica2222 suncica2222 is offline Offline
Light Poster

why is this simple code using 100% CPU?

 
0
  #1
Jun 13th, 2009
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:

  1. //import java.awt.event.WindowEvent;
  2. //import java.awt.event.WindowListener;
  3. import java.awt.*;
  4.  
  5. //import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.event.*;
  9.  
  10. import javax.swing.*;
  11. //import javax.swing.JPanel;
  12.  
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16.  
  17. public class WinLsnrExp extends JFrame {
  18.  
  19. private boolean a;
  20.  
  21.  
  22. public WinLsnrExp() {
  23.  
  24.  
  25. setTitle("Suncica2222");
  26. setSize(150, 150);
  27. KlasaPanel panel = new KlasaPanel();
  28.  
  29. //Container panel = getContentPane();
  30. panel.setBackground(Color.green);
  31. add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
  32. //repaint();
  33. panel.repaint();
  34.  
  35.  
  36.  
  37. //panel1.setBackground(Color.red);
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. }
  46. }
  47.  
  48. class BrojiKlikove
  49. {
  50. public static int a=0;
  51. public BrojiKlikove()
  52. {
  53. a++;
  54. System.out.println(a);
  55. for (int i=0;i<a-4;i++){
  56. CekajSekundi y= new CekajSekundi(0);
  57. KlasaKojaSlusa x=new KlasaKojaSlusa();
  58. }
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. class KlasaKojaSlusa extends JFrame
  66. {
  67. private static boolean b;//nicemu ne sluzi
  68.  
  69. public KlasaKojaSlusa(String s){}
  70.  
  71. public KlasaKojaSlusa()
  72. {
  73. WinLsnrExp wle = new WinLsnrExp();
  74.  
  75. //wle.setTitle("Suncica2222 ");
  76. //wle.setSize(150, 150);
  77. //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  78.  
  79. wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  80. wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
  81. wle.setVisible(true);
  82.  
  83.  
  84.  
  85. wle.addWindowListener(new WindowAdapter()
  86. {
  87.  
  88. public void windowClosing(WindowEvent arg0) {
  89. System.out.println("Window Closing");
  90. //this.b=true; // a cannot be resolved or is not a field
  91. BrojiKlikove x=new BrojiKlikove();
  92. }
  93. });
  94. }// konstrukt
  95. }
  96.  
  97. class KlasaPanel extends JPanel
  98. {
  99.  
  100.  
  101.  
  102. public void paintComponent(Graphics g)
  103. {
  104. super.paintComponent(g);
  105. //g.Font(50);
  106.  
  107. Font font = new Font("Serif", Font.PLAIN, 100);
  108. setFont(font);
  109. //g.setColor(Color.red);
  110. g.drawString("xD",10, 90);
  111.  
  112. }
  113.  
  114.  
  115. }
Last edited by suncica2222; Jun 13th, 2009 at 3:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: why is this simple code using 100% CPU?

 
0
  #2
Jun 13th, 2009
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?
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: suncica2222 is an unknown quantity at this point 
Solved Threads: 0
suncica2222 suncica2222 is offline Offline
Light Poster

Re: why is this simple code using 100% CPU?

 
0
  #3
Jun 13th, 2009
offcourse

those 2 classes are very simple


  1. public class KlasaZaMain
  2. {
  3.  
  4.  
  5.  
  6. public static void main(String[] args) throws Exception
  7. {
  8.  
  9.  
  10. for (int i=0;i<5;i++){
  11. KlasaKojaSlusa x=new KlasaKojaSlusa();
  12. CekajSekundi y= new CekajSekundi(0);
  13. //x.praviProzor();
  14.  
  15.  
  16. //y=new KlasaKojaSlusa("xaxaxa");
  17. //KlasaKojaSlusa.brojiKlikove();
  18. }
  19. /*while(true){
  20.  
  21.   Thread.sleep(500);
  22.   }*/
  23.  
  24. //if (KlasaKojaSlusa x==null)
  25. System.out.println(BrojiKlikove.a + "izbrojao sam");
  26.  
  27. }
  28.  
  29. }//klasa


  1. public class CekajSekundi {
  2. public CekajSekundi(int sekunde) {
  3. try
  4. {
  5. //wait(sekunde*1000);
  6. Thread.sleep(sekunde*1000);
  7. }
  8. catch (Exception e) {}
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: why is this simple code using 100% CPU?

 
0
  #4
Jun 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: suncica2222 is an unknown quantity at this point 
Solved Threads: 0
suncica2222 suncica2222 is offline Offline
Light Poster

Re: why is this simple code using 100% CPU?

 
0
  #5
Jun 13th, 2009
it pop ups 5 widows when started

when it's set to draw green background and "xD" string on the panel it uses 100% CPU

when it's set to blank windows it's ok and uses 5% or less

you can copy that code in to eclipse and see it for yourself,press F11

it's drawing that mess it up
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: why is this simple code using 100% CPU?

 
0
  #6
Jun 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: suncica2222 is an unknown quantity at this point 
Solved Threads: 0
suncica2222 suncica2222 is offline Offline
Light Poster

Re: why is this simple code using 100% CPU?

 
0
  #7
Jun 14th, 2009
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%

  1. //import java.awt.event.WindowEvent;
  2. //import java.awt.event.WindowListener;
  3. import java.awt.*;
  4.  
  5. //import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.event.*;
  9.  
  10. import javax.swing.*;
  11. //import javax.swing.JPanel;
  12.  
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16. //class just for frame and panel painting
  17.  
  18. public class WinLsnrExp extends JFrame {
  19.  
  20. private boolean a;
  21.  
  22.  
  23. public WinLsnrExp() {
  24.  
  25.  
  26. setTitle("Suncica2222");
  27. setSize(150, 150);
  28. KlasaPanel panel = new KlasaPanel();
  29.  
  30. //Container panel = getContentPane();
  31. panel.setBackground(Color.green);
  32. add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
  33. //repaint();
  34. panel.repaint();
  35.  
  36.  
  37.  
  38. //panel1.setBackground(Color.red);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. }
  47. }
  48.  
  49. //class that counts clicks on red X closing button of the window
  50.  
  51. class BrojiKlikove
  52. {
  53. public static int a=0;
  54. public BrojiKlikove()
  55. {
  56. a++;
  57. System.out.println(a);
  58. for (int i=0;i<a-4;i++){
  59. CekajSekundi y= new CekajSekundi(0);
  60. KlasaKojaSlusa x=new KlasaKojaSlusa();
  61. }
  62. }
  63. }
  64.  
  65.  
  66. //class that makes the window frame and has a listener on red X //closing button
  67.  
  68. class KlasaKojaSlusa extends JFrame
  69. {
  70. private static boolean b;//nicemu ne sluzi
  71.  
  72. public KlasaKojaSlusa(String s){}
  73.  
  74. public KlasaKojaSlusa()
  75. {
  76. WinLsnrExp wle = new WinLsnrExp();
  77.  
  78. //wle.setTitle("Suncica2222 ");
  79. //wle.setSize(150, 150);
  80. //wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  81.  
  82. wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  83. wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
  84. wle.setVisible(true);
  85.  
  86.  
  87.  
  88. wle.addWindowListener(new WindowAdapter()
  89. {
  90.  
  91. public void windowClosing(WindowEvent arg0) {
  92. System.out.println("Window Closing");
  93. //this.b=true; // a cannot be resolved or is not a field
  94. BrojiKlikove x=new BrojiKlikove();
  95. }
  96. });
  97. }// konstrukt
  98. }
  99.  
  100. //class for drawing "xD" string on panel
  101. //I would also like to draw some more strings but it wont let me...
  102.  
  103. class KlasaPanel extends JPanel
  104. {
  105.  
  106.  
  107.  
  108. public void paintComponent(Graphics g)
  109. {
  110. super.paintComponent(g);
  111. //g.Font(50);
  112.  
  113. Font font = new Font("Serif", Font.PLAIN, 100);
  114. setFont(font);
  115. //g.setColor(Color.red);
  116. g.drawString("xD",10, 90);
  117.  
  118. }
  119.  
  120.  
  121. }


class just for calling everything in main()



  1. public class KlasaZaMain
  2. {
  3.  
  4.  
  5.  
  6. public static void main(String[] args) throws Exception
  7. {
  8.  
  9.  
  10. for (int i=0;i<5;i++){
  11. KlasaKojaSlusa x=new KlasaKojaSlusa();
  12. CekajSekundi y= new CekajSekundi(0);
  13. //x.praviProzor();
  14.  
  15.  
  16. //y=new KlasaKojaSlusa("xaxaxa");
  17. //KlasaKojaSlusa.brojiKlikove();
  18. }
  19. /*while(true){
  20.  
  21.   Thread.sleep(500);
  22.   }*/
  23.  
  24. //if (KlasaKojaSlusa x==null)
  25. System.out.println(BrojiKlikove.a + "izbrojao sam");
  26.  
  27. }
  28.  
  29. }//klasa

class which constructor make pause of int seconds in running program

  1. public class CekajSekundi {
  2. public CekajSekundi(int sekunde) {
  3. try
  4. {
  5. //wait(sekunde*1000);
  6. Thread.sleep(sekunde*1000);
  7. }
  8. catch (Exception e) {}
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: suncica2222 is an unknown quantity at this point 
Solved Threads: 0
suncica2222 suncica2222 is offline Offline
Light Poster

Re: why is this simple code using 100% CPU?

 
0
  #8
Jun 15th, 2009
any one?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: why is this simple code using 100% CPU?

 
0
  #9
Jun 15th, 2009
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC