Paul Sisco

Greenhorn
+ Follow
since Jun 15, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Paul Sisco

Originally posted by Nitesh Kant:
If i see a system that creates 130 threads to do some work i will make sure that it never gets into production.
Increasing the number of threads like that will not increase performance. Infact it will decrease it because of thread scheduling overheads. In this sort of a scenario your OS will only be busy swapping threads and before the thread can start work it will be swapped out to give the cpu to a different thread.

My suggestion is to use a threadpool. You have it readily available in jdk5 in the concurrent package. A backport of the same is also available for prior jdk 5. (just google for java backport concurrent)



I agree completely! You should do some tests with thread pools using different numbers of threads. You will see performance improve up to a point, then performance will degrade as you add more threads. The job being performed by each thread is a big factor, so the right number of threads for best performance can be different for different systems.

Another thing to consider is whether you want new threads for each execution, or if you want the threads started once and they will pull from work from a queue.

It will be a fun exercise, and you will learn quite a bit from it.

Originally posted by Arjun Reddy:
Hi,

Do runtime exceptions need a try-catch blocks at all? Coz for ArithmeticException(Runtime Exception), even tho I did not put it in the try catch blocks, I was able to compile the code properly(It gave me an error at runtime.Even tho I put em in the try-catch, it still gives me an error at runtime only). But for SqlException(Compile time exception), if I don't have a try-catch blocks, It's giving me a compile time error.

Thanks.

[ June 26, 2008: Message edited by: Arjun Reddy ]



The reason to have a try/catch is so you can handle the exception how ever you want, instead of letting another part of the system handle it for you. There are times when you want to handle the exception locally. In that case, a try/catch is what you need. There are other cases where you want your method to throw an exception. This way whatever called that method can handle the exception.

The method you are calling that requires the try/catch at compile time is explicitly defined to throw an exception. So you are required to either catch it, or define your method to throw it.

I hope this makes sense. Please let me know if it doesn't, and I will try again.
15 years ago

Originally posted by Seamus Minogue:
Deepa,

Have you tried stepping through the code yet?

The problem is this:
arr[i] = 0;

The way this for next works is for each iteration "i" will BE the next value in the array. Not the index of the next item in the array.

So stepping through it:

Iteration 1:
i = 1
set arr[1] = 0
arr = 1,0,3,4

Iteration 2:
i = 0
set arr[0] = 0
arr = 0,0,3,4

Iteration 3:
i = 3
set arr[3] = 0
arr = 0,0,3,0



Then you print them correctly giving you 0 0 3 0


:-)




In addition to the above comment, arrays are zero based. Just keep in mind that you start counting at zero. So your array looks a bit like this...if we were counting, that is...
element 0 = 1
element 1 = 2
element 2 = 3
element 3 = 4
15 years ago

Originally posted by Ramesh Kumar Swarnkar:
Hi Paul,


= No.
Sorry, if my question gives that impression.May be would have been more specific.

But, from above posts I come to a conclusion that:
if a method is 'static-synchronized', then to work on this method, one has to acquire Class Lock.



But, if the method is 'synchronized', then to work this method, one has to acquire Object Lock. And this implies to 'synchronized block' also.



That is right. Using the sinchronized(){} block will synchronize on the object passed in, and it is just a way to have multiple locks at at the same time, within the same object. I have found it usefull for session objects that can be called my more than one other object at the same time.
Sinchronized methods are probably cleaner. Keeping it simple is good.

Originally posted by Ramesh Kumar Swarnkar:
Hi Friends,
Need to know if a class (say class A) has two 'synchronized' methods, then is it possible to access each of these 'synchronized' methods by two separate object(say instance of class B and class C) ?

As per theory in java:
Every Class has a Lock.
Every Instance has a Lock.
Every Object has a Lock. (am not sure about the instance lock and object lock; whether they are same or different !? ).

Anyway, does this theory mean:

to work on any synchronize method an object need to acquire the lock of class(?) OR object/instance(?).
Can anybody please elaborate it.


thanks in advance !!

[ June 22, 2008: Message edited by: Ramesh Kumar Swarnkar ]



Just to add my two cents....

Please let me know if I did not understand the question completely.

I took this to mean that you wanted to call the different methods on the same instance of a class. If the methods are synchronized, they are synchronized on the class, and the will not run at the same time. If you use a synchronized block within the methods, they will run at the same time.

If I make a small class like this:

Then I made a couple of threads to call the same instance of the above class 1000 times each. Thread 1 called method 1, and thread 2 called method 2.
This is a sample of the output:
Method 2 Start: 1214250298180
Method 1 Start: 1214250298180
Method 2 End: 1214250298273
Method 2 Start: 1214250298273
Method 1 End: 1214250298273
Method 1 Start: 1214250298273
Method 2 End: 1214250298367

Notice that the start and end lines overlap. If the methods are synchronized, each method call would need to end before the other one could begin.
[ June 23, 2008: Message edited by: Paul Sisco ]

Originally posted by Avi Sridhar:


Thanks everyone for your thoughts. it really helped.

I am going to use this instead



and we will get 60 chars there.



This isn't exactly about your question. Hopefully I am not out of place asking this...
Are you sure the string length will always be 60? you should probably use a variable for the string length in place of the number 60. This way you can handle any length string up to the limit of your int array.
15 years ago

Originally posted by GUNA RANJAN:
Thank You all the way which i worked is that correct ! if not denote my mistakes !

Swapping three variable without using temp variable

a = 2 b = 1 c = 3

a = a+b+c = 6

b = a -b-c = 2
c = a -b- c = 1
a = a-b-c = 3

After swaping

a = 3 ; b = 2 ; c = 1






This looks like a neat trick, but is there a real world application for it? I don't think I have ever considered doing anything like this in any language.
15 years ago

Originally posted by Sravan Patti:
Hi,

I wanted to whether the use of variables in an interface is good practice or not. This question was asked in one of my interview.
Can also explain me the reason if it is NOT a good practice??

Thanks



Variables defined in an interface are constants, so they are implicitly public static final variables. They need to be initialized when they are defined.

I could see it being used for something like paymentType for a payment interface, but I would probably just put the constants for that in a base class, not in the interface.



This thread discusses the same issue:
Java Ranch - Instance variables in interface

Paul
15 years ago

Originally posted by Prabhat Gupta:

As i per my knowledge this is not DUPLICATE posting.

I know that the string that i am using it not valid one . But,the problem is when your are browsing some file in Windows OS , you will get that INVALID java string. Before processing i have to convert it as "C:\\Documents and Settings\\prabhat_kumar04\\Desktop\\Prabhat\\System.txt" or
"C:/Documents and Settings/prabhat_kumar04/Desktop/Prabhat/System.txt"

But the problem remains same that you are getting the inavlid String as input. So, what should i do now??




When you use getPath on the File Object, you should not get an error related to the escape character. You only need to escape the "\" character when you are using it in a string literal defined in the code.



This is the output:
c:\out\Test.txt
c:/out/Test.txt

Can you post some of the code to show how you are getting the filename?


Paul
15 years ago
JSP

Originally posted by Muni Sammy:
Most of Indian Service based companies are keeping Notice period as Three months, do you think is this a loyal .... Eventhough if you don't have any dependency you have to be their for 3 months ....

I learned in America most of company have a one day Notice ....then why 3 months here ...



Two weeks is the custom in America. Some people give more, and some give less. I Have given from two to four weeks notice when I left previous jobs, and I set the date of my last day.

I am not familiar with the customs in India, but 3 months seems excessive. It sounds like the company holds all the cards, and you, the employee, are their property.

You definately need to pay attention to the contract you signed when you started with them. My current contract says that either I or my employer can terminate my position without warning. It's a small world, so I wouldn't try it.
15 years ago

Originally posted by JIGAR THAKOR:
Here I am getting the data as I posted earlier.

This is not JDBC or query related question.

I want to achieve groupism of user_ids by server_ids.
key = server_id
values stored in list = user_id.

Please post it on the General Java - Difficult




I'm not completely sure which problem you have, but I'll take a shot anyway.

It sounds like your hashmap looks like this?
HashMap<String, ArrayList> idMap = new HashMap<String, ArrayList>();

This would give you a hashmap of unique server_id's where each server_id key has 1 ArrayList associated with it. That ArrayList would be the last one added to the HashMap.

If your database is giving you the same server_id multiple times and each time it can have a different list of user_id's then you could do something like this:

String newServerId = ...
ArrayList newUserList = ...
ArrayList userList = (ArrayList)idMap.get(newServerId);
if(userList == null){
//add the newServerId and newUserList to the hashMap
}else{
//add the newUserlist to the userList you got from the hashmap.
}


This could certainly give you duplicate user_id's in the arrayLists. If that is a problem, try using a Set instead of an ArrayList for the user_id's in the idMap.

Set<String> userIdSet = new HashSet<String>();
HashMap<String, Set<String>> idMap = new HashMap<String, Set<String>>();


I hope this helps.

Paul