Skip to main content

Java - working with Threads



In one of my last posts, i created a Spring based rest api project and hosted it on Heroku. If you would like to read about it, please see below posts -



I was working on a similar project at work where I was using Siebel java databean to create a rest api interface into our Siebel application. I wanted to test the performance of this application. This was more of a quick unit test and I decided to do this by running a Java application with multiple threads calling the API at the same time.

I am posting below the sample program to show how I did it. It is a very basic tutorial giving an example of using threads in Java. It also shows how to make HTTP calls from a java application.

First I will setup a class that will make my HTTP call to the Heroku api I setup -


As you can see in the above class, I am implementing the Runnable interface with it. This is the interface that will help me run it as a thread later. All the code that I want to execute in the thread is within the Run method that I am overriding.

Here I am using the HttpsURLConnection class to make a get call to my heroku web app url in the Run method. Another thing I am doing is capturing the time before the call and after it and finding the difference. This will give me an idea of how much time each call took.

Now let me setup the Main class which will run my threads. I want to run around 10 threads making the call to this api end point.  So lets do that below in a very dumb looking main class -

I called it dumb as it really doesnt do much. All it does was create 10 threads with instances of my other class. Then i started all of them one after the other.

Below you can see the output of each of the calls. Looks like my api on heroku was responding with 500ms each of the 10 times. Not bad performance at all. Of course it is a very simple API with very little data. However I hope you still found this post useful to see how threads can be created and used in Java. Enjoy!

Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com...

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Sales...

Summer 16 Salesforce Administrator Maintenance exams

I was able to clear my Summer 16 Salesforce administrator maintence exam earlier today. Next I need to start working on Winter 17 immediately. Anyway before that i thought i will leave some pointers to any folks who may be taking Summer 16 anytime soon Below are some topics you can read about to prepare yourself for the exam - 1. Process builder improvements -  Process builder can now execute immediate actions and then evaluate the next criteria in the flow. Please see this link - https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_forcecom_process_multiple_actions.htm Expect a question on this. 2. Files sharing permission -  In Classic, when a file is shared to a user via a record, its access can be set to "Set by Record". This allows the record level access to determine access level for the file as well. Read about this below - https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_files_access_by_record.htm 3. Chatter Q...