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



Polymorphic Methods in java

Polymorphism is an object-oriented programming concept that literally is being in many shapes. polymorphism allows you to define one interface and have multiple implementations. Polymorphism in Java has two types: Compile time and Runtime. When you are overloading the methods it's static polymorphism and when you are overriding methods it's dynamic polymorphism.

 

1- static polymorphism

public class Calculator
{
    public int DoIt(int a, int b )
    {
        return a+b;
    }

    public int DoIt(int a, int b, int c)
    {
        return a+b+c;
    }

    public int DoIt(int a, double b)
    {
        return a+(int)b;
    }
}

 

 

2 - dynamic polymorphism


public class Machine{

    public void Action(){
        System.out.println("it moves!");
    }
}

public class Car extends Machine{

    @Override
    public void Action(){
        System.out.println("it moves and can carry 4 person");
    }
}

public class Bus extends Machine{

    @Override
    public void Action(){
        System.out.println("it moves and can carry 40 person");
    }
}



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 and I'm spending a lot of my time on architecture subject. 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&nb 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
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC