working on it ...

Filters

Explore Public Snippets

Sort by

Found 123 snippets matching: synchronization

    public by lbottaro modified Sep 2, 2013  246943  2  7  0

    Lock and synchronization in python

    This example shows how to deal with lock in python parallel programming. Here we want to calculate the disk occupancy for a given directory in Linux OS, evaluating the disk occupancy for any subdirectory using threads. Note the usage of lock mechanism to ensure atomic operation on the total sum.
    import os
    import string
    import threading
    from threading import Thread
    
    disk_usage = 0
    lock = threading.Lock()
    
    for dir in os.popen('ls -d *').readlines():
        print "Dir: " + dir
        t = Thread(target=methodToBeExecutedInParallel, args=(dir,))
        t.start()
        #Join all existing threads to main thread.
        for thread in threading.enumerate():
            if thread is not threading.currentThread():
              thread.join()
        print "Disk usage = " + str(disk_usage)
    
    # Function invoked by each thread
    def methodToBeExecutedInParallel(directory):
        print "Working on dir = " + directory
        cmd = 'du -s ' + directory
        line = os.popen(cmd).readlines()[0]
        dirUsage = int(line.split()[0])
        print "Acquiring lock for "+directory+"..."
        with lock:
            print "Acquired lock for "+directory
            global disk_usage
            disk_usage += dirUsage
        print "Disk usage = " + str(dirUsage)

    public by lbottaro modified Sep 4, 2013  3070  2  6  0

    Basic code to manage threading in python

    This simple example shows how to launch N threads in parallel and handle their execution. Please note the object Thread, where you can define the method to be executed and parameter to be passed. The main program will launch threads and wait for all to complete before continuing execution.
    import datetime
    import threading
    from threading import Thread
    
    my_data_List = ["abc", "123", "bar", "foo"]
    for data in my_data_List:
          print "Going to start thread for data = " + data
          t = Thread(target=methodToRunInParallel, args=(data,))
          t.start()
          #Join all existing threads to main thread.
          for thread in threading.enumerate():
              if thread is not threading.currentThread():
                thread.join()
          print "Completed!"
    
    def methodToRunInParallel(data):
         print "Working on data = " + data
         #Do some stuff here...
         time.sleep(60)
         

    public by lbottaro modified Jul 3, 2012  2331  0  4  0

    Python Multithread synchronization

    How to synchronize threads in Python code. The threading module provided with Python includes a simple-to-implement locking mechanism that will allow you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method the new lock object would be used to force threads to run
    !/usr/bin/python
    
    import threading
    import time
    
    class myThread (threading.Thread):
        def __init__(self, threadID, name, counter):
            self.threadID = threadID
            self.name = name
            self.counter = counter
            threading.Thread.__init__(self)
        def run(self):
            print "Starting " + self.name
            # Get lock to synchronize threads
            threadLock.acquire()
            print_time(self.name, self.counter, 3)
            # Free lock to release next thread
            threadLock.release()
    
    def print_time(threadName, delay, counter):
        while counter:
            time.sleep(delay)
            print "%s: %s" % (threadName, time.ctime(time.time()))
            counter -= 1
    
    threadLock = threading.Lock()
    threads = []
    
    # Create new threads
    thread1 = myThread(1, "Thread-1", 1)
    thread2 = myThread(2, "Thread-2", 2)
    
    # Start new Threads
    thread1.start()
    thread2.start()
    
    # Add threads to thread list
    threads.append(thread1)
    threads.append(thread2)
    
    # Wait for all threads to complete
    for t in threads:
        t.join()
    print "Exiting Main Thread"

    public by cghersi modified May 12, 2014  2883  1  6  1

    Test harness for Multithread lock management strategies

    This test suite provides a simple way to check the performances of locks vs. synchronized syntax in case of multithread safe run of a task. The parameters are added as static variable at the top of the class, MAX_LOOPS is the number of times the task is performed, and MAX_SET_SIZE is the number of simultaneous threads launched and insisting on th
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    public class SafeLocks {
    
    	private static int MAX_LOOPS = 1000;
    	private static int MAX_SET_SIZE = 10000;
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		System.out.println("Starting ReentrantLock test bench at " + System.currentTimeMillis());
    		
    		TestSuite suite1 = new ReentrantlockEx();
    		testBench(suite1);
    		
    		System.out.println("Starting Synchronized test bench at " + System.currentTimeMillis());
    		
    		TestSuite suite2 = new SynchronizedEx();
    		testBench(suite2);
    
    		System.out.println("END");
    	}
    	
    	private static void testBench(final TestSuite test) {
    		for (int thIdx = 0; thIdx < MAX_SET_SIZE; thIdx++) {
    			Thread t = new Thread(new Runnable() {			
    				@Override
    				public void run() {
    					long summedSpentTime = 0;
    					for (int i = 0; i < MAX_LOOPS; i++) {
    						long startTime = System.currentTimeMillis();
    						test.doStuff(i);
    						summedSpentTime += (System.currentTimeMillis() - startTime);
    					}
    					test.addTime(summedSpentTime);
    				}
    			});
    			t.start();
    		}
    		
    		while (test.threadsFinished() < MAX_SET_SIZE) {
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		
    		List<Long> times = test.getTimes();
    		long totalTime = 0;
    		for (long l : times) {
    			totalTime += l;
    		}
    		System.out.println("Suite finished: # of threads: " + MAX_SET_SIZE + "; # of running cycles of single task: " + 
    				MAX_LOOPS + "; avg time is " + (totalTime / times.size()));
    	}
    	
    	public static abstract class TestSuiteImpl implements TestSuite {
    
    		private ArrayBlockingQueue<Long> runTimes = new ArrayBlockingQueue<Long>(MAX_SET_SIZE + 1);
    
    		public void addTime(long time) {
    			runTimes.add(time);
    		}
    		
    		public List<Long> getTimes() {
    			List<Long> res = new ArrayList<>();
    			runTimes.drainTo(res);
    			return res;
    		}
    		
    		public int threadsFinished() {
    			return runTimes.size();
    		}
    	}
    	
    	public static class ReentrantlockEx extends TestSuiteImpl implements TestSuite {
    		private ReentrantLock lock = new ReentrantLock();
    
    		public final boolean doStuff(int number) {
    			boolean result = false;
    			lock.lock();
    			
    			//do something not completely stupid so that compiler will not get rid of it
    			String test = "abc0";
    			result = (test.hashCode() == number);
    			
    			lock.unlock();
    			return result;
    		}
    	}
    	
    	public static class SynchronizedEx extends TestSuiteImpl implements TestSuite {
    		
    		private final Object syncObj = new Object();
    
    		public final boolean doStuff(int number) {
    			boolean result = false;
    			synchronized (syncObj) {
    				//do something not completely stupid so that compiler will not get rid of it
    				String test = "abc0";
    				result = (test.hashCode() == number);
    			}
    			return result;
    		}
    	}
    	
    	public static interface TestSuite {
    		public boolean doStuff(int number);
    		public void addTime(long time);
    		public List<Long> getTimes();
    		public int threadsFinished();
    	}
    
    }

    public by cghersi modified Mar 18, 2014  2540  1  6  2

    Safe Realization of a Thread in Java

    Manage threads in Java can be tricky, most of all the stop of them. With this class no littering thread will be left behind.
    import java.lang.Thread;
    import java.util.List;
    
    public class MySafeThread implements Runnable {
    
    	private static MySafeThread singleton = null;
    	/**
    	 * Synchronization variable for the safe instantiation of the singleton
    	 */
    	private static Object synchSingleton = new Object();
    	/**
    	 * The actual thread object
    	 */
    	private Thread thread;
    	/**
    	 * The flag signalling whether the thread is running or not
    	 */
    	private boolean threadRunning = false;
    
    	
    	private MySafeThread(){
    	}
    
    	public void restart() {
    		closeThread();
    		startThread();
    	}
    
    
    	/**
    	 * Closes the thread, releasing all the resources
    	 */
    	public void closeThread(){
    		threadRunning = false;
    
    		//break the execution of the thread and wait until the last loop is finished:
    		Thread moribond = this.thread;
    		this.thread=null;
    		moribond.interrupt();
    
    		singleton = null;
    	}
    
    	
    	/**
    	 * Retrieves the singleton instance of the class; if
    	 * the singleton has not yet been instantiated, it builds 
    	 * a new instance using the protected constructor.
    	 * @return The singleton instance of the class
    	 */
    	public static MySafeThread get(){
    		if (singleton == null) {
    			synchronized (synchSingleton) {
    				if (singleton == null) {
    					singleton = new MySafeThread();
    				}
    			}
    		}
    	
    		return singleton;
    	}
    
    	/**
    	 * The main method of the thread which listens for new responses available, and
    	 * sends them via IPC
    	 */
    	public void run(){
    		Thread t = Thread.currentThread();
    		
    		threadRunning = true;
    		while(threadRunning) {
    			//do something...
    		}
    	}
    
    	/**
    	 * Starts the thread
    	 */
    	public void startThread(){
    		//only a single instance of the thread can run:
    		if (threadRunning)
    			return;
    	
    		this.thread = new Thread(this, "GiveANameToTheThread");
    		this.thread.start();	
    	}
    }

    public by cghersi modified May 12, 2014  2337  0  7  2

    safe use of locks in Java

    This class shows how to safely implement a lock to manage a crucial variable, accessed by several threads in concurrency. Just a notes on the use of ReentrantLock: its use may be replaced by a simple synchronized block where no huge concurrency issues are forecasted (say, less than 700 concurrent threads)
    import java.util.concurrent.locks.ReentrantLock;
    
    public abstract class LockedClass {
    
    	/**
    	 * Determines whether or not a call (i.e. a particular method of the class)
    	 * should be always sent, without depending from the implementation 
    	 * of isChildSendable() in the children classes
    	 */
    	private boolean isAlwaysSendable = true;
    	/**
    	 * Lock for the guard of sending mechanism.
    	 */
    	private ReentrantLock lock = new ReentrantLock();
    	
    	public LockedClass(boolean isAlwaysendable) {
    	  this.isAlwaysendable = isAlwaysendable;
    	}
    
    	protected boolean isChildSendable(){
    	  //override this behavior in children classes if you need...
    		return true;
    	}
    
    	public final boolean isSendable(){
    		if (isAlwaysSendable)
    			return true;
    		
    		boolean b;
    		lock.lock();
    		b = isChildSendable();
    		if (b)
    			setChildSendable(false);
    		lock.unlock();
    		return b;
    	}
    
    	protected void setChildSendable(boolean sendable){
    	  //put something here or override in children classes if you want to 
    	  //customize
    	}
    
    	public final void setSendable(boolean sendable){
    		if (isAlwaysSendable)
    			return;
    		
    		lock.lock();
    		setChildSendable(sendable);
    		lock.unlock();
    	}
    }

    public by MarcoDeMagistris modified Apr 5, 2013  2418  4  5  0

    My First Snippet: How to create a singleton in C#

    This is the first example of a snippet: - the title represents in few words which is the exact issue the snippet resolves; it can be something like the name of a method; - the description (this field) is an optional field where you can add interesting information regarding the snippet; something like the comment on the head of a method; - the c
    public class MyClass 
    { 
       private static MyClass s_current; 
       private static object s_objSync = new object(); 
    
       private MyClass() {} 
    
       public static MyClass Instance 
       { 
           get 
           { 
               if (s_current == null) 
               { 
                   lock (s_objSync) 
                   { 
                       if (s_current == null) 
                           s_current = new MyClass(); 
                   } 
               } 
               return s_current; 
           } 
       } 
    } 

    public by snip2code modified Aug 13, 2017  45  0  2  0

    First Snippet: How to play with Snip2Code

    This is the first example of a snippet: - the title represents in few words which is the exact issue the snippet resolves; it can be something like the name of a method; - the description (this field) is an optional field where you can add interesting information regarding the snippet; something like the comment on the head of a method; - the c
    /* place here the actual content of your snippet. 
       It should be code or pseudo-code. 
       The less dependencies from external stuff, the better! */

    public by cghersi modified Mar 18, 2014  2540  2  7  1

    Manage a queue in Java

    This simple class has a queue that is filled and depleted with its two methods. The getter blocks until a new item is ready on the queue. It can be used also as a synchronization mechanism between two threads
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Observable;
    import java.util.Observer;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class ClassWithQueue {
    
    	private ArrayBlockingQueue<String> incomingTextMessages;
    	
    	public StubIPCJ(){
    		incomingTextMessages = new ArrayBlockingQueue<String>(1000);
    	}
    	
    	public String getTextMessage(){
    		String msg = null;
    		try {			
    			msg = incomingTextMessages.take();
    		} catch (InterruptedException e) {
    			log.error("ipcGetFrame failure: " + e.getMessage());
    			return null;
    		}
    			
    		return msg; 		
    	}
    
    	public void addToQueue(String obj) {		
    		synchronized(incomingTextMessages){
    			try {
    				incomingTextMessages.add(obj);
    			}catch(IllegalStateException ew){
    				incomingTextMessages.poll();
    				incomingTextMessages.add(obj);
    			}
    		}								
    	}
    }
    

    public by giovanni.gambaro modified Jun 27, 2017  422  1  3  1

    Date formatting using ThreadLocal variable

    How to avoid synchronization for a not thread safe classes
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Foo
    {
        // SimpleDateFormat is not thread-safe, so give one to each thread
        private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
            @Override
            protected SimpleDateFormat initialValue()
            {
                return new SimpleDateFormat("yyyyMMdd HHmm");
            }
        };
    
        public String formatIt(Date date)
        {
            return formatter.get().format(date);
        }
    }
    • Public Snippets
    • Channels Snippets