Using Thread.sleep(); in sysout and syserr [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I did a little experiment to try to arrange the order of the printing of sysout and syserr to the console because it's mixing sometimes. I tried this:
System.out.println("output");
Thread.sleep(100); //Wait before printing the error to insure the sysout comes first.
System.err.println("Error");
and it worked fine. But I read questions here about sysout and syserr printing out of order and this is not suggested. I'm just wondering is using thread.sleep(); in situations like this bad? I'm using eclipse and in my project I put thread.sleep before every syserr in my code.

Do a flush() instead of a sleep().
System.out.println("whatever");
System.out.flush();
...
(added in response to OP's comments)
Within the single thread a cross-mix of printing cannot happen.
It must be that one of the other threads in the five other places is printing at the same time. You'll need to synchronize on something. e.g., write a little utility method
public static void printToOutAndErr(String toOut, String toErr) {
synchronized(System.out) {
System.out.println(toOut);
System.err.println(toErr);
}
}
And have them all use that.
However, let me strongly strongly suggest that you look into a logging framework instead. They cover all this stuff for you.

As you don't appear to be using more than a single thread, that should work fine. A Concurrency refresher may be of help.

Related

Is it legitimate to have business logic inside a finally block? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a method entangled with control structures. It has many ways to exit. Before leaving the method I need to do some final processing. Instead of repeating the same logic before each exit or refactoring that logic in a method and calling it several times it seem handy to leave that in a finally block. Is it really a legitimate use of finally or am I abusing it?
finally is there for a reason, to add logic that must be execute before the exiting block
It's a valid choice for a method if you don't want/need to use AOP/AspectJ
Notice you may have to use finally for release resources as Connection
For example you can use it when you must audit/log or do autonomous transaction at the end of the method
As #DaveNewton comment, in some cases there might be a better way of refactoring/separating logic, but you can't ignore that it's a valid usage

I want to give security to my PHP code from copy [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want such a script or logic so that if somebody has a copy of my code, they can only access/run it if they have my unique key, otherwise they get an error. Is there any way to do like this?
Thanks
If somebody has a copy of your PHP code they can run it. As PHP is not compiled, the possessor can read and edit the code as well; so even if you put in something that checks against a secret key, they could simply remove it. If you encrypt the code into an unreadable state, it’s also in an un-runnable state.
So, in brief, there really isn’t a way to give working PHP to another person in a way that they can’t simply run it. If you’re looking to sell a product of some sort, your best bet is probably to run it as a service so the end user never actually sees the code.
The closest you might hope for would be to make it difficult to read, i.e. with meaningless variable and function names and zero white space; but that won’t stop somebody who really wants it, (and who knows how to work some basic refactoring utilities), and only adds significant complication to your own work

I'm new at programming. I'm not sure how much to use methods [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I just finished my first program, but I haven't use any methods in it, since I'm just beginning to learn how to use them. Here's the original code : http://codepad.org/JiBfJI8Q I started to fractionate it but realised that it would be a method inside another all the way down. Is that actually the way to do it, or did I get the idea wrong?
without having looked at your code:
The general idea of methods is to separate small
portions of code which might be used at multiple other places in your code.
so yes, calling methods from within other methods is a good thing to do.
ideally your so called "composed methods" read out like a little story:
public void transaction(){
openDatabaseConnection();
addRecordsToDatabase();
closeDataseConnection();
}

make a GET call 100,000 times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a requirement where I am writing a small utility to test apis(ofcourse there are existing tools but it has been decided to write one). I am required to bombard the api, for the same api call, with say 100 threads, around say 100,000 times.
I am using 'PoolingHttpClientConnectionManager' for the making the calls. I am using something as mentioned in the below link:
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
My question is:
(1) How can I run the above code for 100,000 iterations? Using that many number of threads is obviously a bad idea. Initially thought of using ExecutorService for maintaining thread count and number of jobs to be submitted but it felt redundant.
(2)I read about 'setMaxTotal'(max connections) and 'setDefaultMaxPerRoute'(concurrent connections) but I dont think it will help achieve(1) though I will obviously be required to increase the values.
Please advise. Thanks in advance.
You could use a threadpool and push the workerfunction the required number of times. Then you could even vary the number of workerthreads executing the functions to simulate different loadsituations.
Threadpool tutorial:
https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
Why don't you use Jmeter for such performance/load testing?

How to call a Thread in a method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to programmatically move a Thread in another method. How I can do this?
that's entirely not possible in Java.
There are some ways around that - using tools like AtomicReferences, AtomicBoolean, wait/notify or Channels. With these tools, you could inform the other thread that it should do something specific.
Another approach would be to copy SwingUtilities invokeLater - like here: http://www.javamex.com/tutorials/threads/invokelater.shtml
However, I would like to ask the question why that method execution needs to be run in a specific thread? Wouldn't just another (new thread) be fine too? That should significally simplify your problem. In that case, just start a new thread to call that method

Categories