Doubt in thread handling
I have a doubt in the thread mechanism.....
Below is a sample of the class
public class SimpleThread extends Thread {
private int countDown = 5;
private static int threadCount = 0;
public SimpleThread() {
super("" + ++threadCount); // Store the thread name
start();
public String toString() {
return "#" + getName() + ": " + countDown;
public void run() {
while(true) {
System.out.println(this);
if(--countDown == 0) return;
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new SimpleThread();
There is a while loop checks for true.... I am not able to understand which it is referring to .
and i want to run the thread 5 times .... is it possible to run this using while loop... Because when i use while the loop never end...
Your guidance is much more appreciated...
Thankyou
Shasi
Hi,
sorry, but your question seems to be a little confusing.
shasi wrote:
I have a doubt in the thread mechanism.....Do you actually have a doubt or more something of a question? To me, it seems to be more of a question...
Below is a sample of the classPlease use the code tags to format your code for better readability...
public class SimpleThread extends Thread {
private int countDown = 5;
private static int threadCount = 0;
public SimpleThread() {
super("" + ++threadCount); // Store the thread name
start();
public String toString() {
return "#" + getName() + ": " + countDown;
public void run() {
while(true) {
System.out.println(this);
if(--countDown == 0) return;
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new SimpleThread();
There is a while loop checks for true.... I am not able to understand which it is referring to .What should it be referring to? The while loop has to be given some statement that evaluates to a boolean. This could either be something like a > 10 or simply true.
and i want to run the thread 5 times .... is it possible to run this using while loop... Because when i use while the loop never end...You do run the Thread five times. Or are you talking about executing the loop in your run() method for five times (which you also happen to do already)?
What do you want to run using a while loop?
The while loop will end: with each iteration, your countdown variable is being decremented, and as soon as it reaches zero, the return statement is executed breaking out of the while loop.
Bye.
Similar Messages
-
Creative Cloud Leaks Thread Handles
I have Creative Cloud version 1.8.0.447 running on windows 7, and I notice that it leaks a Thread Handle, about 4 times, or so, per minute. Since I leave my computer on most of the time, after a few days, the number of leaked Thread Handles is up in the tens of thousands.
I can see the thread handles leaking using Process Explorer.
Generally when a thread ends, its handle needs to be closed, or else the handle will stay active forever, or until the process ends. It looks like the programming team is not closing these handles after the threads end.
Please, adobe, fix this problem in a future release of creative cloud.
-ToddSome 10.9.x links
-next link says After Effects, but check YOUR permissions !!!
-http://blogs.adobe.com/aftereffects/2014/06/permissions-mac-os-start-adobe-applications.ht ml
-Mac 10.9.4 and OpenCL issue https://forums.adobe.com/thread/1514717
-Mac 10.9.3 workaround https://forums.adobe.com/thread/1489922
-more Mac 10.9.3 https://forums.adobe.com/thread/1491469
-and https://forums.adobe.com/thread/1507936
or
A chat session where an agent may remotely look inside your computer may help
Creative Cloud chat support (all Creative Cloud customer service issues)
http://helpx.adobe.com/x-productkb/global/service-ccm.html -
Secondary Indices and free-threaded handles
As per the BerkeleyDB documentation, BDB manages synchronization if the db_handle is free-threaded (i.e opened with flag DB_CREATE).
Example: Consider a free-threaded handle 'dbp' to the primary index and a free-threaded handle 'dbs' to the secondary index associated with 'dbp'. If one thread is trying to write using 'dbp' and another thread is trying to read using 'dbs', then does BDB maintain synchronization ?As per the BerkeleyDB documentation, BDB manages
synchronization if the db_handle is free-threaded
(i.e opened with flag DB_CREATE).
Example: Consider a free-threaded handle 'dbp' to
the primary index and a free-threaded handle 'dbs'
to the secondary index associated with 'dbp'. If one
thread is trying to write using 'dbp' and another
thread is trying to read using 'dbs', then does BDB
maintain synchronization ?Would you please clarify your question especially if my guess at what you are asking is incorrect?
I can't tell if you are really talking about a handle or a cursor and you also mentioned a secondary index. .
DB handles are free-threaded if the DB_THREAD flag is specified to the DB->open method when the database is opened or if the database environment in which the database is opened is free-threaded. The handle should not be closed while any other handle that refers to the database is in use; for example, database handles must not be closed while cursor handles into the database remain open, or transactions that include operations on the database have not yet been committed or aborted.
You will also want to configure locking, see the DB_INIT_LOCK flag. Therefore you will likely want to configure a Berkeley DB Environment with the DB_INIT_LOCK | DB_THREAD flags added to your environment.
If that was NOT what you were asking about please clarify your question or tell us what you are trying to accomplish?
Ron Cohen
Oracle Corporation -
Dear all,
The question is as the follow:
Condition
Class A purpose: Get Data from SQL server 2000 e.g.
SQLDB transaction = new SQLDB();
transaction.getData();
Now, I would like to handle the transaction time in order to show that it is processing with Swing component: JLabel, JButton and JFrame. How to write a swing program? Please help me or give me some reference. Thank you so much for your help!
Regards,
kzyoIf java 6, this is an article which I think will probably help you immensely:
http://java.sun.com/developer/technicalArticles/javase/swingworker/
It's all about how to implement SwingWorker, a background threader that can among other things give progress reports to the GUI.
Message was edited by:
petes1234 -
Once exported the object to RMI service, it allows many client object to invoke the methods on RMI object.
It can allow multiple invoke at the same time. Does it mean that RMI service handle multiple request by multi-thread?
Can we control the number of request on RMI object? or It should be done by developer?You can do that because the previous answer was flat 100% 180 degrees wrong. RMI is and always has been multithreaded in all versions and implementations I have ever seen or heard of in over ten years.
This bizarre rumour to the contrary surfaces occasionally. It frankly baffles me how anybody could believe it for a moment. Such a system would be almost completely useless. -
Hello. I'm having a bit of a problem with the way I'm handling threads, quite obviously. At the moment, it appears that each thread I have running in my program uses a little over 10MG of memory. Is this normal? Assuming it is, is there any common and good method of handling threads that might reduce this?
My program currently requires a new thread to be created every time a new user logs in... which I believe is the only way it can be written because it requires input from each user on an unpredictable basis. That being said, even so much as 10 users would require 100MG plus my other executing threads... looking at 100 is thus rather rediculous... and I'm trying to create my program to allow for far more than 100. I know it can be done, so I'm rather confused as to why it is that I'm not doing it.
Thank you so much for your time,
-JessThanks so much everyone for the replies. I started up a thread pool, as suggested, and that seems to be working well, so thanks for the idea... it doesn't, however, seem to account for the memory problems. this leads me to believe that perhaps there's something about Java I'm not understanding...
When I run my program, and then enter a ps from the command line, I get the following output: (which, is essentially 10 processes that each use up 0.6% of my host's memory... which is a total of 100MB. Thus accounting for 10 MB per process)
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
26088 pts/2 S 0:00 1 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26089 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26090 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26091 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26092 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26093 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26094 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26095 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26096 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.Server
26100 pts/2 S 0:00 0 56 260499 13636 0.6 java -classpath /home/src/classes engine.ServerPreviously, whenever a new user logged in, a new process would be created identical to the above, which is why I assumed that each process was a thread. So, in preliminary testing, I created a command which runs through the ThreadGroups of my main class (engine.Server), and then prints out the listing. The code is something like:
ThreadGroup group = server.getThreadGroup();
ThreadGroup parent = group.getParent();
while ( parent!=null )
group = parent;
parent = group.getParent();
group.list();This gives me a result of 6 threads:
java.lang.ThreadGroup[name=system,maxpri=10]
Thread[Reference Handler,10,system]
Thread[Finalizer,8,system]
Thread[Signal Dispatcher,9,system]
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
Thread[UpdateHandler,5,main]
Thread[pool-1-thread-1,5,main]However I have ten processes running, which are consuming a pretty large amount of memory. I previously assumed that there were four other threads from a different thread group which were unaccounted for... but I'm not entirely sure that this is the case anymore.
Is there anything that strikes someone out there that could be accounting for 10 processes consuming 10MB each? I apologize for my ignorance, as I come from a C background where there was only ever one process when I ran (more or less) this same program... so I'm rather lost.
As well, I have a few statistics from my Runtime, but I think that is unneccesary. If it would help, I will post it, however.
Thanks so much for your help,
-Jess -
Hi Techies,
Actually I am having one problem with TableModel and Threads. I have one server application which creates one thread for each client. It has one table that contains one row representing details of one client connection. It adds or deletes one row when client make or close connection with server respectively. The data is fed into the table through a list. The list is updated as a connection is added or lost. Here the server class has one static variable which will be incremented when ever any client closes the connection. It works fine when the clients are closed with some interval between them. But when two or more clients close the connection quickly, then the table shows corrupted data. One row data will appear in two rows. I think even before the other threads are updated themselves for first closed connection, the second connection close causes the data to be corrupted. I think there is some problem of multi threading. Can any one suggest any solution or guidance for this?
Edited by: JavaBreather on 18 Apr, 2009 4:33 PMJavaBreather wrote:
Hi Techies,
Actually I am having one problem with TableModel and Threads. I have one server application which creates one thread for each client. It has one table that contains one row representing details of one client connection. It adds or deletes one row when client make or close connection with server respectively. The data is fed into the table through a list. The list is updated as a connection is added or lost. Here the server class has one static variable which will be incremented when ever any client closes the connection. It works fine when the clients are closed with some interval between them. But when two or more clients close the connection quickly, then the table shows corrupted data. One row data will appear in two rows. I think even before the other threads are updated themselves for first closed connection, the second connection close causes the data to be corrupted. I think there is some problem of multi threading. Can any one suggest any solution or guidance for this?
Edited by: JavaBreather on 18 Apr, 2009 4:33 PMI guess your code needs some kind of synchronization (synchronized) block. Either use synchronized list (even though I don't prefer this) or synchronize the block of code (operation) which updates table data. -
Doubt in String handling...
Hi
Here i have a code snippet.
public class StringComp
public static void main(String[] args)
String a = "abc";
String b = "def";
String c = "abcdef";
String d = "xyz";
String e = "xyz";
a+=b;
System.out.println("Value of a = :" + a);
System.out.println("Value of c = :" + c);
System.out.println("Value of d = :" + d);
System.out.println("Value of e = :" + e);
if(a==c)
System.out.println("TRUE");
else
System.out.println("FALSE");
if(e==d)
System.out.println("TRUE");
else
System.out.println("FALSE");
if i compile and run this code snippet i am getting the result as
Value of a = :abcdef
Value of c = :abcdef
Value of d = :xyz
Value of e = :xyz
FALSE
TRUE
here i add the value of a and b to a. so a=abcdef, value of c also abcdef.
but if i check (a==c) the result is False why??
Can anyone tell me how the string is handled in java (behind the scene) where they are stored ? and how the == operator works on String?
thanks in advance
chithrakumarStrings are pooled. This means that if you writeString a = "1";
String b = "1";you have a good chance that a == b.
For new Strings (as "abc" + "def" returns), the pool is bypassed. This explains why "abc + "def" != "abcdef". To make sure a String is pooled, use intern (). ("abc" + "def").intern () == "abcdef", guaranteed.
This is all very opaque behaviour and you shouldn't really rely on this in your code, except in very rare circumstances (extreme performance concerns or if you're stuck with an identity hash map [not that that's very likely]). -
How do you dispose a thread-handled modal dialog not thru some actions?
The code almost looks like this:
/* Thread that disposes the dialog when the time in seconds is "9" */
class AboutThread extends Thread {
private volatile Thread about;
AboutDialog ad;
AboutThread(AboutDialog ad) {
this.ad = ad;
public void stopped() {
about = null;
ad.dispose();
ad = null;
System.gc();
public synchronized void run() {
Thread thisThread = Thread.currentThread();
about = thisThread;
System.out.println("About thread is running!");
ad.start();
while (about == thisThread) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.err.println("Thread.sleep error: " + ex);
String s = new String(getCurrentDateTime("s"));
if (s.equals("9")) {
ad.setVisible(false);
ad.setModal(false);
ad.setVisible(true);
System.out.println(9);
this.stop();
/* Shows a dialog describing the User Log application */
public class AboutDialog extends Dialog implements ActionListener {
public AboutDialog(Frame parent, String title) {
super(parent, title, false);
Panel labelPanel = new Panel();
labelPanel.setLayout(new BorderLayout());
labelPanel.setBackground(Color.gray);
JLabel jlab = new JLabel("User Log 1.0");
jlab.setHorizontalAlignment(SwingConstants.CENTER);
jlab.setFont(new Font("Monospaced", Font.BOLD, 28));
JLabel jlab1 = new JLabel("Copyright(c) 2001 Soft Wares. All Rights Reserved.");
jlab1.setHorizontalAlignment(SwingConstants.CENTER);
labelPanel.add(jlab, "Center");
labelPanel.add(jlab1, "South");
add(labelPanel, "Center");
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.gray);
Button okButton = new Button("OK");
okButton.addActionListener(this);
buttonPanel.add(okButton);
add(buttonPanel, "South");
setSize(400, 130);
setLocation(parent.getLocationOnScreen().x + 200,
parent.getLocationOnScreen().y + 200);
public void start() {
show();
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
at.stopped();
}ooops! i'm sorry. in the AboutDialog Class, it should be "super(parent, title, true)" for it to be modal.
anyway, it seemed that posting the partial code above of the whole app is not so understandable.
what i like to address here is that: how do i dispose or get rid of the thread-dispatched modal dialog by not making mouse clicks or any other user intervention? i wanted it to be disposed by the same thread, which dispatched it, when a certain variable value (global or local) is met. is this possible? -
Hai
Is it possible by the use of threads to run two processes concurrently( i mean in the literal sense that two processes need to run at the same time) ?
Thank youDepends. If you mean really at the same time, yes, but you need the hardware support for that. If you mean time slices or something, then yes, that's what threads are there for, anyway.
-
Hi,
I've been poking around in the site but haven't been able to find the answer: does Java dispatch threads to different cores when it has control of the processor(s) or does the JVM internally decide which thread gets control of the processor(s)?
Thanks.Thanks...I seemed to remember that Java decided but I wasn't sure and was in fact correct.
Speaking of which - can you perhaps link me or tell me what terms to search under so I can get some Sun documentation on this and read up about it? I am most interested in the details.
Thanks ! -
Hi Friends,
I am a beginner in Java.So plz excuse for my ignorance.
Qa .1. Extending the Thread class**
public class BasicThread1 extends Thread {
public void run() {
//........My code
public class Main extends Thread {
public static void main(string... arg){
Thread thread = new BasicThread1();
thread.start();
How run method invokes while calling thread.start(); ?
QA 2. Implement the Runnable interface**
public class BasicThread2 implements Runnable {
public void run() {
public class Main{
public static void main(string... arg){
Runnable runnable = new BasicThread2();
Thread thread = new Thread(runnable);
thread.start();
How run method invokes while calling thread.start(); ?
ThanksDinil wrote:
In Thread class start method shows
public synchronized void start() {
This method is not invoked for the main method thread or "system"
group threads created/set up by the VM. Any new functionality added
to this method in the future may have to also be added to the VM.
A zero status value corresponds to state "NEW".
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
private native void start0();I am invoking start method without parameters ( Thread.start();)
Then how run() method invokes from start() ?That is the magic that happens in the native start0() method. -
File file=new File("myfile.txt");
file.createNewFile()
file.isDirectory()
Here file.createNewFile() needs a try/catch block or need throws declaration in the method signature to compile where as file.isDirectory() is just fine.I expect file.isDirectory() to behave the same way as file.createNewFile() behaves.Can anyone explain thisFrom the Javadocs:
Returns:
true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise
So, no need to throw an exception if the thing doesn't exist.
As an aside - why did you pick this forum to post this question to? I'm just curious. -
Doubt in Radiobutton handling in Module-pool program
Hi Experts,
I have requirement in where in a module pool screen I am creating two Radio-buttons say R1 and R2 under one group.
When I do this the first radio button R1 is by default gettimg checked. What is that I need to do for making secand radiobutton R2 by default getting cheked.....?
Please note that it is in Module-pool not a Report.
Kindly help me out in this aspect.
Regards,
Rama Murthy.PHi,
You can set check/uncheck value of radio buttons in PBO.
But that check box button should be declared in main program(se38) also.
for e.g
data: R1 type c,
R2 type c.
declare above in your main program.
now go to se51. In layout go to Dictionary/Program fields or press F6.
Now click on Get from program button and drag R1,R2 to your layout.
Now right click on it and under convert option you will get radio button.
In main program under PBO make that radio button check.
Loop at screen.
if screen-name = 'R2'.
R2 = 'X'.
endloop.
Modify Screen.
Regards,
SUDHIR MANJAREKAR -
Doubt in Radiobutton handling in Module-pool
Hi Experts,
I have requirement in where in a module pool screen I am creating two Radio-buttons say R1 and R2 under one group.
When I do this the first radio button R1 is by default gettimg checked. What is that I need to do for making secand radiobutton R2 by default getting cheked.....?
Kindly help me out in this aspect.
Regards,
Rama Murthy.PHi,
You can try out the following.
In you ABAP code declare a Global Data Element of the same type as the Screen Element. Then create a Module in the PBO of the calling screen. There you write the following statement.
<data element2> = 'X'.
Then after the execution of the PBO the ABAP and screen elements will automatically gets transfered. This will mark the second radio button as checked.
Let me know if require any more details.
Thanks,
Samantak.
Rewards points for useful answers.
Maybe you are looking for
-
We recently started suffering an issue with our CSS11501S-K9 units not performing URL stickiness on our SSL wrapped L5 rules. I've spent dozens of manhours working on the problem, and have quite a bit of information to report, including a solution.
-
Reg. Stock account posting instead of consumption account
Dear Experts Pls consider my below senario As per SAP the material goes to consumption account when GR is made. but our concern wants to keep the material at unrestricted stock and that is to be used as and when required for production. how to config
-
Crashing when creating tagged pdf book
I am trying to save a book to pdf using the tagged pdf option on the Tags tab of the PDF setup dialog. It works fine except when there is a foldout graphic (36.5 x 11). I get the following error: Internal Error: 10014, 7686376, 7686666, 10069197 a
-
Hi , I have a set of dblinks is there any way for me to find out who are the users and what are the tables being accessed by these dblinks ? this is for Oracle8i & 9i pls advise tks & rgds
-
Urgent please respond... JTree Speceific Node Coloring
hi i am facing a problem.I want to create root node with different color anf font and its child node with different color and font.When I change the color of my tree the whole tree nodes are given the same color.Is there any way in which i can color