Sunday, September 27, 2015

Jython Multi Threading in wsadmin

What is the Multi threading?


Multi threading is similar to how multiple programs runs on your computer system, moreover which run at same time. When you are running multiple tasks in parallel on same CPU[machine]. More cores parallel processing allowed by your VM. The simple mapping let me tell you that if you have 1 core CPU that links to Multiple threads.

Do's and Don'ts in threading

Let us talk about does first here:
  1. You defined tasks which are independent of each other
  2. Physics/collision calculations in on thread, graphics in another
  3. All the threads should communicate back with the main thread which actually starts them
Don'ts
  1. If you don't have clarity on multi threading then whole program would mess up! so be-careful

Simple Threading examples on wsadmin

The following sample is from tutorial point python examples.
import threading
import time

exitFlag = 0

class LearnThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        print_time(self.name, self.counter, 5)
        print "Exiting " + self.name

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            thread.exit()
        time.sleep(delay)
        print "%s: %s" % (threadName, time.strftime('%X'))
        counter -= 1

# Main begins here
#Create new threads
thread1 = LearnThread(1, "DevDeploy-1", 1)
thread2 = LearnThread(2, "DevDeploy-2", 2)

# Start new Threads
thread1.start()
thread2.start()

print "Exiting Main Thread"
Here my idea is to use the thread for a development environment deployments. The deployment task you make in a module of function that can be called in run() method of thread would work independently and performs the desired task. My Sample output is as follows:
wsadmin>execfile('F:/pavanbsd/Jython/ThreadEx2.py')
Exiting Main Thread
Starting DevDeploy-2
Starting DevDeploy-1
wsadmin>DevDeploy-1: 21:42:51
DevDeploy-2: 21:42:52
DevDeploy-1: 21:42:52
DevDeploy-1: 21:42:53
DevDeploy-2: 21:42:54
DevDeploy-1: 21:42:54
DevDeploy-1: 21:42:55
Exiting DevDeploy-1
DevDeploy-2: 21:42:56
DevDeploy-2: 21:42:58
DevDeploy-2: 21:43:00
Exiting DevDeploy-2

No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow