2

I am an experienced java developer.Of now I have started learning multithreading in java.I am able to grasp most of the concepts but still I am unable to visualize applications running on threads.I know the concept but somewhere if I am given a problem statement, I am unable to think or visualize it in threads.How should my approach be in order to start visualizing applications in threading and start coding the applications in threads?

closed as too broad by user40980, gnat, amon, GlenH7, Thomas Owens Aug 29 '14 at 12:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    recommended reading: Where to start – gnat Aug 29 '14 at 7:01
  • I think I understand your question. The first step is stop using vector, and use something like List instead. The next step is perhaps initializing with enough cache from info boxes, so that when a web container like Tomcat or GlassFish starts, things can run ok. Perhaps one should also think about less use of modularized Unit. Multi threading is a better way to make sure things go well. – InformedA Aug 29 '14 at 7:04
1

The easiest way is to think of threads as processes (and this is the case in some unixy systems that spawn a new process where some systems would create a new thread).

A process is a blob of code and data with a stack and a single thread anyway.

Once there, you can start to imagine sharing data between threads- its easier in a single process with multiple threads, but dangerous. Thinking of each thread as a "process" will help visualise the importance of understanding where shared data is managed.

2

I usually think about thread as an arrow that execute your code(Like when you're debugging on Eclipse or Netbeans);
This little arrow executes your code, but instead of having just one, you'll spawn another one.

In Java, you have to let a class implements the Runnable interface that lets you implement the run() method;
When you want to spawn a new thread that runs in parallel than the first one you have to open an object Thread, passing the object that implements Runnable and start it.

public class SaimLol implements Runnable {
    @Override
    public void run() {
        while(true) { 
            doSomethingInSaimLol();
        }
    }
}


public class Main {
    public static void main(String... args) {
        SaimLol myObject = new SaimLol();
        Thread threadSaim = new Thread(myObject);
        threadSaim.start();

        doSomethingInMain();
    }
}

What is the thread programming goal?
When you start the thread calling the threadSaim.start() a new "arrow" spawns and the code will be executed calling concurrently the methods doSomethingInSaimLol() and doSomethingInMain();
With threads you can do things like control two parts of your code concurrently, or more advanced things like build a semaphore to let know the first thread if all variables are setted and it can goes.

1

I think the best way to deal with parallel programming is to keep in mind the global data you're dealing with and not so much the code you're writing on. As soon as you stumble upon global data (such as a member variable), you have to think to yourself: Is it possible that this variable get's accessed by multiple threads?

The advantage of this approach is that you're thinking about what's essential to multithreading, which is accessing global data, and not your code. It doesn't matter what code is accessing it; it could be the code you're currently typing, or a code somewhere else. I don't think about whether my code will be executed in parallel, I'm just caring about the variable I'm acessing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.