Ehsan Ghanbari

Experience, DotNet, Solutions

Multithreading in java

I'm not a java developer and I've never worked with java in real products. Sometimes I read and concepts and compare with C#, Java is cool as well! By using multithread you can perform many operations together and the system doesn't block the user until completing the operations. There are two ways for multithreading in java: inhering from a class named Thread and implementing an interface named Runnable. I like Runnable because you have another chance to inherit the class from another class if you would need!

 

public class Multithread implements Runnable  {

    public void run() {

        System.out.println("Thread: " + Thread.currentThread().getId() + " is running");

    }

}

 

class MainClass

{

    public static void main(String[] args)

    {

        int n = 5;

        for (int i=0; i<5; i++)

        {

            Thread object = new Thread(new Multithread());

            object.start();

        }

    }

}

 

If you run this code, the following output will appear:

 

Thread: 8 is running
Thread: 9 is running
Thread: 10 is running
Thread: 11 is running
Thread: 12 is running

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I'm spending a lot of my time on software architecture. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus