Jump to content
Tuts 4 You

The BETTER language for multithreaded programming?


hotpockets

Recommended Posts

Posted (edited)

So I'm fairly familiar with multiple languages, my programming foundation is pretty strong. I wrote a program using Java (just because it was the quickest language to make a mock up for me at least) and it's creating 5 threads. Each thread is shared with 1 HttpClient (Apache) which sends/receives JSON commands, and my computer begins to get really slow if I run too many threads. I'm looking for efficiency and speed. There was a really good article about Java vs C# on Google, and I've been sold on C#. I need the language to be compatiable with both mac/windows (it's all console, no gui). And yes, my computer is up to par, it's very powerful.

Which languages would be best suited for the task of running multiple threads and sending REST/JSON calls with the least amount of lag? My options are either Objective C, Swift, C#, Java, and C++ because I've worked with those languages before. 

Edited by evo85
Extreme Coders
Posted (edited)

Spawning a new thread per request is not a very efficient design and not scalable as you can already see. You need to fix the maximum number of threads that can be run at any given time. In Java you can do this using a ThreadPoolExecutor service. You can combine this with non blocking IO (1 & 2) if you have a large number of connections most of which are idle at the cost of increased code complexity.

Coming to languages, atm although Swift has been open-sourced is not available on Windows unless you use something like Silver from RemObjects. C++ is not quite the type of language used for web programming. So your best bets are on the remaining.

Edited by Extreme Coders
Posted (edited)

Each thread logins, sends a retrieve product request. I need to spawn multiple threads because during high traffic hours the servers will get 5xx errors. Is there another efficient way? Basically imagine each thread is the same user repeating the same exact requests. 

 

Here's the example I pulled off Apache's page.

http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/connmgmt.html#d5e405

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        .build();

// URIs to perform GETs on
String[] urisToGet = {
    "http://www.domain1.com/",
    "http://www.domain2.com/",
    "http://www.domain3.com/",
    "http://www.domain4.com/"
};

// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i < threads.length; i++) {
    HttpGet httpget = new HttpGet(urisToGet);
    threads = new GetThread(httpClient, httpget);
}

// start the threads
for (int j = 0; j < threads.length; j++) {
    threads[j].start();
}

// join the threads
for (int j = 0; j < threads.length; j++) {
    threads[j].join();
}
static class GetThread extends Thread {

    private final CloseableHttpClient httpClient;
    private final HttpContext context;
    private final HttpGet httpget;

    public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
        this.httpClient = httpClient;
        this.context = HttpClientContext.create();
        this.httpget = httpget;
    }

    @Override
    public void run() {
        try {
            CloseableHttpResponse response = httpClient.execute(
                    httpget, context);
            try {
                HttpEntity entity = response.getEntity();
            } finally {
                response.close();
            }
        } catch (ClientProtocolException ex) {
            // Handle protocol errors
        } catch (IOException ex) {
            // Handle I/O errors
        }
    }

}
Edited by evo85
Posted

@evo85

Quote

repeating the same exact requests. 

Is some caching technique already in place?

Regards,
Tony
 

Posted

Because you are looking for language that works on both Windows and MAC, I recommend using

Delphi XE8

Yes, It has amazing Threading capabilities and portable to several platforms easily.

you can still enjoy the high performance of C++ and so many other advantages.

  • Like 1
Posted

I don't believe caching is required, reason being is the program is ran for only maybe 10 - 20 minutes. It isn't an entire deal breaker for it to work on Mac, if it has to be cut for optimal performance it's no problem. Also, I can quickly write a the program for swift.

Posted

In this case, the language u use isn't as important as the library u use. HttpClient C#/Java/WinInet/etc are all slow as sh!t. There's a lot of paid http libs out there for windows that can give u ASM/C web crawling speed in languages like C#, Java, C++ etc, via native .dll's called. 

If you don't want to pay, then writing ur own from scratch in C++ or native Obj C is probably the fastest. Googles crawlers, probably the web application w/largest user base in world, is written in C++. For raw speed though ASM will almost always win.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...