Rohan Dhapodkar

Greenhorn
+ Follow
since Jun 27, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Rohan Dhapodkar

Deepak Bala wrote:

Now, if you ever come across a situation where incrementing / decrementing counters need to be thread safe across multiple threads, I would suggest the use of atomic variables. They simplify this process greatly.

http://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html



I implemented this solution using AtomicIntegers @ http://rohandhapodkar.blogspot.in/2012/04/print-odd-and-even-numbers-using-two.html
12 years ago
One of the best way is to
1. load heap dump with Eclipse MAT.
2. In the histogram view select char[] class
3. Right click ->'Show objects by class' -> 'By incoming references'

and then class references view will show you how many instances of char[] are referred by which class and their size.

I learned this hard way
12 years ago
Calling grand parent method is not part of java language specifications but with simple bytecode manipulations using ASM you can do it. Here is solution @ http://rohandhapodkar.blogspot.in/2012/03/call-grand-parent-method-in-java.html
13 years ago
Using NIO features ( like ServerSocketChannel, Selector) introduced in java 1.4 you can handle multiple requests ( I think this can be done by ServerSocket as well, but not tested ).

I created sample program which accepts multiple requests, new thread is assigned per request( this can be optimised using ThreadPool), Thread just sleeps for 10 seconds (just to demo that i can handle multiple requests).



Stephan van Hulst wrote:If a client with some IP address connects to a server on a specific port, a socket is created. If the same IP address does this again, it will fail because the socket already exists.

If another IP address connects to the same port, it will create a different socket. But this is no different from handling separate requests on different ports.


I even tried to test what Stephen already quoted. I initially opened multiple tabs in firefox everyone pointing to http://localhost:8085/test and surprised, firefox normally opens one http connection at time. I then tried to use telnet localhost 8085 and found than I can open 2 simultaneous telnet connection and nothing is blocked.
I suspected about firefox behaviour and then i just modified url in every tab to include request param ( just to make sure every tab will submit different http requests) eg, http://localhost:8085/test?1...12 . I opened 12 tabs everyone with different parameters and i found that firefox use to create 6 parallel connections and after closing first connection it submits next connection (but max 6 at a time).

Look likes for same url, instead of creating multiple http connection, browser used to tunnel http requests on same connection and max 6 can be the limitation set by browser for any host/site.
I even verified this limitation of 6 by creating single html page with 6 iframes and hit at same time from both firefox and chrome browser. I can see now 12 active connections in tcpmonitor tool (i used tcpmonitor here to track timing and request and response).
Hi Hemang,
Thread dumps obtain on windows/linux normally contain native thread id as nid= which is missing in your thread dump.
eg.


Once you have this native thread id in your java stack trace, you can use Process Explorer ( google it) tool, to identify which specific native thread of your java process is consuming the CPU. You can then correlate thread id from your native java process to your java stack trace thread (nid is hex value).

I learnt this in one of the training.
JDBC Performance :
One point many developers forgot while using plain jdbc or even hibernate (which internally uses JDBC API's) is properly setting fetchSize on JDBC Statement, improves throughput considerably (about 10x). Default fetch size is 20, which means in single network call, JDBC driver will fetch only 20 records and next 20 records in another network call. What if i am reading millions of records in those 4-5 hours ? Increasing fetch size from 20 to 500 or even 1000 will reduce IO/Network calls by 250 or 500 times. This will definitely need more memory for store those 1000 records in memory but i can ignore them for throughput.
Above mentioned point is definitely worth trying. Another interesting point is fetching CLOB/BLOB in resultSet. Using CLOB/BLOB in resultSet will force JDBC driver (alteast about oracle I know) to fetch only one record per network call. Hence fetching BLOB/CLOB will result in more network calls.If BLOB/CLOB is not used, remove them from resultSet and you will see performance improved.
13 years ago
You can use Camel here. By defining camel paths, in future you can easily change underline transport protocol from ftp to sftp or file. it will just a matter of few configuration changes and no code changes.

Check camel examples - http://camel.apache.org/ftp2.html

eg. from("ftps://admin@localhost:2222/public/camel?password=admin&ftpClient.trustStore.file=./src/test/resources/server.jks&ftpClient.trustStore.password=password").to("bean:foo");
13 years ago

Harsha Smith wrote:without semaphore, cyclic barrier, the following code produced desired result for several runs.



This code is not perfect. Check results given below. Also what if i don;t want to put any sleep ? Above mentioned code execution is entirely based on Thread scheduler and it's not guaranteed to work always.
Try your code with just 3 threads and you will see difference.

0 Thread-0
0 Thread-1
0 Thread-2
1 Thread-0
1 Thread-1
1 Thread-2
2 Thread-0
2 Thread-1
2 Thread-2
3 Thread-0
3 Thread-1
3 Thread-2
4 Thread-0
4 Thread-1
4 Thread-2
5 Thread-0
5 Thread-1
5 Thread-2
6 Thread-0
6 Thread-1
6 Thread-2
7 Thread-0
7 Thread-1
7 Thread-2
8 Thread-0
8 Thread-1
8 Thread-2
9 Thread-0
9 Thread-1
9 Thread-2
10 Thread-0
10 Thread-1
10 Thread-2
11 Thread-0
11 Thread-1
11 Thread-2
12 Thread-0
12 Thread-1
12 Thread-2
13 Thread-0
13 Thread-1
13 Thread-2
14 Thread-0
14 Thread-1
15 Thread-0
15 Thread-1
14 Thread-2
16 Thread-0
16 Thread-1
15 Thread-2
17 Thread-0
17 Thread-1
16 Thread-2
18 Thread-0
18 Thread-1
17 Thread-2
19 Thread-0
19 Thread-1
18 Thread-2
19 Thread-2


Even with just 2 threads out put is not perfect.
Check this
0 Thread-0
0 Thread-1
1 Thread-0
1 Thread-1
2 Thread-0
2 Thread-1
3 Thread-0
3 Thread-1
4 Thread-0
4 Thread-1
5 Thread-0
5 Thread-1
6 Thread-0
6 Thread-1
7 Thread-0
7 Thread-1
8 Thread-0
8 Thread-1
9 Thread-0
9 Thread-1
10 Thread-0
10 Thread-1
11 Thread-0
11 Thread-1
12 Thread-0
12 Thread-1
13 Thread-0
14 Thread-0
13 Thread-1
15 Thread-0
14 Thread-1
16 Thread-0
15 Thread-1
17 Thread-0
16 Thread-1
18 Thread-0
17 Thread-1
19 Thread-0
18 Thread-1
19 Thread-1

naved momin wrote:


This is issue with go() method of ChatServer, where you are writing to socket first and then creating read thread. Reorder your code and it will work.

John Vorwald wrote:Try a semaphore with one permit, look for producer / consumer examples...



Semaphore is not perfect solution to this problem. Semaphore is to guarantee that specified number of threads can only enter into restricted area but not their ordering.
Below code will work with high sleep time 100 but if you will reduce sleep from 100ms to 1ms, you can notice a difference.

output with 100 ms.
0 Thread-0
0 Thread-1
1 Thread-0
1 Thread-1
2 Thread-0
2 Thread-1
3 Thread-0
3 Thread-1
4 Thread-0
4 Thread-1
5 Thread-0
5 Thread-1
6 Thread-0
6 Thread-1
7 Thread-0
7 Thread-1
8 Thread-0
8 Thread-1
9 Thread-0
9 Thread-1
10 Thread-0
10 Thread-1
11 Thread-0
11 Thread-1
12 Thread-0
12 Thread-1
13 Thread-0
13 Thread-1
14 Thread-0
14 Thread-1
15 Thread-0
15 Thread-1
16 Thread-0
16 Thread-1
17 Thread-0
17 Thread-1
18 Thread-0
18 Thread-1
19 Thread-0
19 Thread-1

Output with 1 ms.
0 Thread-0
1 Thread-0
0 Thread-1
2 Thread-0
1 Thread-1
3 Thread-0
2 Thread-1
4 Thread-0
3 Thread-1
5 Thread-0
4 Thread-1
6 Thread-0
5 Thread-1
7 Thread-0
6 Thread-1
8 Thread-0
7 Thread-1
9 Thread-0
8 Thread-1
10 Thread-0
9 Thread-1
11 Thread-0
10 Thread-1
12 Thread-0
11 Thread-1
13 Thread-0
12 Thread-1
14 Thread-0
13 Thread-1
15 Thread-0
14 Thread-1
16 Thread-0
15 Thread-1
17 Thread-0
16 Thread-1
18 Thread-0
17 Thread-1
19 Thread-0
18 Thread-1
19 Thread-1

Cyclic Barrier is perfect solution for such problems. This is gauranteed to work with or without Thread.sleep().



output:

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19

sebastian aguirre wrote:but the variable is something that is created and remains more time occupying memory so I'm not sure.



MyObject will be local variable inside your method and memory will be allocated on stack to hold object reference which will be deallocated once thread is out of method. This assignment will not allocate entirely new memory on heap for object.

[ UD: Edited to fix the incorrect quote tag that attributed text to someone other than the actual author. Please be more careful when quoting people. ]
13 years ago
May be i am wrong in interpreting the requirements. But this looks like some ConnectionPooling problem where your connection will be resetted if ideal for some time.
Probably you can have some pool manager which holds ideal connection with timer thread which will run periodically and executes helo() method on all ideal connections. Whenever any client needs any connection, it can request the connection and will return connection after use.
If you can access your server from any X11 based desktop(Windows/Linux), then you can use JVIsualVM which can connect to remote JVM configured with remote JMX settings. Refer http://download.oracle.com/javase/1.5.0/docs/guide/management/agent.html#remote for remote JMX settings.
13 years ago
with Java5, you can use Thread.getAllStackTraces(), to get list of all threads and then you can check status of thread using Thread.getState()