Dot meta-character in regex

  • The Dot meta-character matches any single character, without caring what that character is
  • Note : Dot does not matches the new line character.
  • Dot is not a meta-character in character set [ ]
  • Dot is a very powerful regex metacharacter and it allows you to be lazy i.e you put a dot and everything matches in regex
  • It is adviced to use Dot meta character sparingly since it matches everything it causes regex to match character that we don’t want to match

Example : If you want to match date format 03/11/2035 you can write a regex like this “\d\d.\d\d.\d\d\d\d” but also matches 03-11-2015 and 03.11.2015
We can improve our regex search for using character class like this “\d\d[/]\d\d[/]\d\d\d\d”

In the above example we don’t have any control of what all character . can match if the above date format input hence it is very dangerous to use dot meta character in regex.

Use Character Sets Instead of the Dot

  • Dot meta character is used most of the time when they know there can be a special character inside the search so you can either blacklist or white-list the charters using character set instead of using dot meta character
  • In the above example of date we can use negated character set to compare date

Q & A

Input : Hello world this is a date 03/11/2035 which has to be found using Regex
But it should not match 03\11\2035 or 03 11 2035 or 03-11-2035
Search : 03/11/2035
================================================================
Regex Using Character set : \d\d[/]\d\d[/]\d\d\d\d
Regex Using Negated Character set : \d\d[^a-zA-Z0-9\-. ]\d\d[^a-zA-Z0-9\-. ]\d\d\d\d

Difference between Encoding and Character set.

For better understand of encoding and character set we will use the UTF-8 encoding and Character set unicode in the below explanation and examples.

So as we all know data is stored on an hard disk in binary format i.e 1 and 0’s. So each system has to read these 1 and 0’s and decode it understand what the data is, so if the data is written on hardisk using UTF-8 encoding then it has to be decoded by UTF-8 decoder to understand the text, any other decoder will convert the text into gibberish. Now the decoded data has to be mapped with a character set which will make the data more readable by humans.

Example : Suppose 1 2 3 4 has to be written on an harddisk then it will be encoded into something like 00000001 00000010 00000011 00000100 using UTF-8 encoder.

Example : Suppose “1101000 1100101 1101100 1101100 1101111” is the data that is stored in the hard disk, now UTF-8 decoding algorithm has to decode it and after decoding it will look something like “104 101 108 108 111”. Now when these numbers are viewed via a unicode character set it will be translated into “hello”

Literal Character Search in Regex

So suppose you have a word to search in a paragraph this is how regex based engine will search it.

Text : Hello World you are learning regex and trying to search a Word in this line.
Regex Search : Word

So the in text ” Hello World you are learning regex” you are searching for word “World” using regex, regex engine will first match for the letter “W” with the letter “H”, since this is not a match it will try matching “W” with the next letter that is “e”, this still not a match then it will proceed with next letter “l” and so on util it reaches the letter “W” at position 7(including space), since it is a match regex engine will now search letter “o” with “letter right next the letter “W” which is “o” it is still a match then the next letter “r” and it is still a match but the next regex word is “d” but the letter right next to “r” in the text is “l” which is not match. so regex engine will now again compare the letter “l” with the first word of the regex that is “W” it is still not a match hence it will proceed with the letter “d” of the text to compare with “W” and search moves on util it it meets letter “W” again.

Note : Regex engines are case sensitive by default

Directory Structure of Linux file system

  • bin – Essential command binaries are stored here
  • sbin – Essential system binaries
  • etc – Contains configuration files
  • home – Any users home directories
  • lib – contains common libraries
  • mnt – Temporary file system file pendrives and cd drives comes under this directory
  • proc – Contains virtual file system and stores kernal info
  • tmp – temporary files are stored in this directory and files in this dir get deleted after restart
  • usr – Contains user program and user data
  • var – (variable)files and folders where system writes during and operation example system logs

Linux Essentails

  • If $ symbol comes up after login in your terminal this means you have logged in as a user
  • If # symbols comes up after login in you terminal means you have logged in as root user
  • su (super user)command, it is used to login to an user account, if user name is not mentioned then su command will enter into root user.
    su tyson
  • whoami – print which user you have logged in as in linux
  • passwd – is the command to change the password of login
  • id – unique id of the user is printer, for root it is zero

Bashrc and BashProfile Specific Change

  • All the terminal specific changes(change color or change PS1) that you want to do can be done in ~/.bashrc file, below code add username@[directory_name] to your terminal
PS1="\u@[\W] $";
export PS1;

locate Command

  • High speed and low impact searching command.
  • Searches index instead of actual system.
  • Indexes are updated each day end by default.
  • Locate won’t know about recently added/deleted files util database is rebuilt.

Update locate database indexes

sudo updatedb

Count of the files using locate

locate -c <file name>

Ignore case in locate

locate -i <file_name>

Show only the file actually exist on file system and not only in db indexes

locate -e <file_name>

Limit output to Number of files to be viewed in result

locate -l 2 <file_name>

You can use less command to scroll over the result

locate <file_name> | less

Fork and Join Framework

  1. Available from Java 7
  2. ForkJoin Pool is a kind of executor service for ForkJoin task
  3. It differs from Executor service by virtue of employing Work-stealing i.e if a worker thread has no task in its pipeline then it will take the task from the task queue of the other busy thread so that the work load is efficiently balanced.
  4. To access the pool
    ForkJoinPool pool = ForkJoinPool.commonPool();
    A static common pool is available for the application and it can be accessed through common pool method.
  5. Creating a new pool
    ForkJoinPool pool = new ForkJoinPool();
  6. The primary thought process behind Fork Join is that each task is recursively split into sub task and executed in parallel using fork method where as the join method will wait for the completion of the task and combines the obtained result.
  7. ForkJoinTaks is an Abstract class that implements Future Interface so that we use it extract results once the task is completed.
  8. ForkJoinTask abstract class is extended into two other abstract classes RecursiveAction and RecusiveTask. These classes are abstract and when we extend these classes we need to override compute() method.
  9. The major difference between RecursiveTask and RecursiveActioin is that RecursiveAction’s computer method does not return any value where as computer method of RecursiveTask returns a value.

Example : To search an element in an array and find its count

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import org.apache.commons.lang3.ArrayUtils;

public class SearchDemo {
	public static void main(String[] args) {
		int arr[] = {20, 10, 20, 30, 20, 50, 40, 80, 60, 70, 90, 50, 20};
		System.out.println(ArrayUtils.toString(arr));
		SearchTask task = new SearchTask(arr, 0, arr.length-1, 20);
		ForkJoinPool pool = ForkJoinPool.commonPool();
		int result = pool.invoke(task);
		System.out.println("Total "+result);
	}
}

class SearchTask extends RecursiveTask<Integer> {
	int[] arr;
	int start;
	int end;
	int search;

	SearchTask(int[] arr, int start, int end, int search) {
		System.out.println("New task created for search from position "+start+" to "+end);
		this.arr = arr;
		this.start = start;
		this.end = end;
		this.search = search;
	}

	@Override
	protected Integer compute() {
		int size = end-start+1;
		int count1,count2,count;
		if(size > 3) {
			int mid = (start + end) /2 ;
			SearchTask task1 = new SearchTask(arr,start,mid,search);
			SearchTask task2 = new SearchTask(arr,mid+1,end,search);
			task1.fork();
			task2.fork();
			count1 = task1.join();
			count2 = task2.join();
			count = count1 + count2;
			return count;
		}else {
			return processSearch();
		}
	}

	private Integer processSearch() {
		int count = 0;
		for (int i = start; i <= end; i++) {
			if (arr[i] == search) {
				count++;
			}
		}
		return count;
	}
}

find command in linux

• Find all the files, directories and sub directories under a directory

find <directory>  
find . #under current directory

• Find only directories or files specificaly

find . -type d #find all the directories under current directory
find . -type f #find all the files under current directory

• Find file with a name

find . -type f -name "test1.txt"  # File search by name
find . -type f -name "text*"      # Wild card search
find . -type f -iname "test1.txt" # Case insensitive search
find . -type f -not -name "*.sh" # File name not ending with *.sh

• All files modified by time search

find . -type f -mmin -10       # Files modified in last 10 minutes
find . -type f -mmin +10       # Files modified more than 10 minutes ago
find . -type f -mmin +1 -mmin -5 # Files modified more than 1 min ago but less than 5 minutes
find . -type f -mtime -10      # Files modified less than 10 days ago
#amin and atime for access minutes and change access days
#cmin and ctime for change minutes and change days

• Find files with size

find . -size +5M          # Files with size more than 5 Mb in size
find . -size +500k        # Files with size more than 500 Kb in size
find . -size  +5G         # Files with size more than 5Gb in size
find . -empty             # Files which are empty

• Find files with permission

find . -perm 777           # Files and folders with 777 permission on them

• Execute a command on the results of find

find demo -exec chown tyson:www-data {} + 

In the above command -exec is the attribute which helps to execute an another command on the results of the find and {} is the place holder that signifies the result of each output of find.
+ sign is under to end the command here, you also use / instead of +

• Find files under only 1 level under current directory (Max Depth)

find . -type f -name "*.txt" -maxdepth 1

Thread local

  1. Thread local class provides thread local variables.
  2. Thread local class maintains values per thread basis.
  3. Each thread local object maintains a separate value like user id, transaction id etc for each thread that access the object.
  4. Thread can access its local value, can manipulate its value and even can remove its value.
  5. In every part of the code which is execute by the Thread we can access its local variable.
  6. Example : Consider a servlet which invokes some business methods, we have a requirement to generate a unique transaction ID for each and every request and we have to pass this transaction id to the business methods. For this requirement we can use thread local to maintain a separate transaction id for every request i.e for every thread.
  7. Thread local class introduced in java 1.2 version and enhanced in 1.5 version
  8. Thread local can be associated with thread scope.
  9. Total code with is executed by the thread has access to the corresponding thread local variable.
  10. A thread can access its own local variables and can’t access other threads local variable.
  11. Once thread entered into dead state all its local variables are by default eligible for Garbage collection.

Constructor :

ThreadLocal tl = new ThreadLocal();
• creates a new ThreadLocal variable

Methods :

• Object get()
Returns the value of thread local variable associated with current thread.

• Object initialValue()
Returns initial value of thread local variable associated with current thread.
The default implementation of this method returns null
To customize our own initial value we have to override this method

• void set(Object newValue)
To set a new value

• void remove()
To remove the value of Thread local variable associated with current Thread.
It is newly added method in java 1.5 version.
After removal if we are trying to access, it will be reinitialized once again by invoking its initial value method.

Example1 :

package com.demo11;

public class ThreadLocalDemo1 {
	public static void main(String[] args) {
		ThreadLocal tl = new ThreadLocal();
		System.out.println(tl.get());
		tl.set("Tyson");
		System.out.println(tl.get());
		tl.remove();
		System.out.println(tl.get());
	}
}

Overriding of initial value method of Thread Local

public class ThreadLocalDemo2 {
	public static void main(String[] args) {
		ThreadLocal tl = new ThreadLocal() {
			public Object initialValue() {
				return "Justin";
			}
		};
		System.out.println(tl.get());
		tl.set("Tyson");
		System.out.println(tl.get());
		tl.remove();
		System.out.println(tl.get());
	}
}

Example of ThreadLocal in Use :

public class ThreadLocalDemo3 {
	public static void main(String[] args) throws InterruptedException {
		Temp x = new Temp();
		Thread t1 = new Thread(x,"ThreadA");
		Thread t2 = new Thread(x,"ThreadB");
		t1.start();
		t1.join();
		t2.start();
	}
}
class Temp implements Runnable{
	int x;
	ThreadLocal y = new ThreadLocal();

	@Override
	public void run() {
		if(Thread.currentThread().getName().equalsIgnoreCase("ThreadA")) {
			x = 100;
			y.set(100);
		}
		System.out.println("Thread Name "+Thread.currentThread().getName()+" Int Value :"+x+" Thread Local Value : "+y.get());
	}
}

A Separate Thread Id for every Thread can be implemented using Thread Local

public class ThreadLocalDemo4 {
	public static void main(String[] args) {
		CustomerThread c1 = new CustomerThread("Thread A");
		CustomerThread c2 = new CustomerThread("Thread B");
		CustomerThread c3 = new CustomerThread("Thread C");
		CustomerThread c4 = new CustomerThread("Thread D");
		c1.start();
		c2.start();
		c3.start();
		c4.start();
	}
}

class CustomerThread extends Thread{
	static Integer custId=0;
	public static ThreadLocal th1 = new ThreadLocal() {
		protected Integer initialValue() {
			return ++custId;
		}
	};
	CustomerThread(String name){
		super(name);
	}
	
	public void run() {
		System.out.println(Thread.currentThread().getName()+" executing with id "+th1.get());
	}
}

ThreadLocal Vs Inheritance (InheritableThreadLocal class)

  • Parent Threads ThreadLocal variable by default not available to the child thread, if we want to make parent threads ThreadLocal variable value available to the child thread then we shlould go for InheritableThreadLocal class.
  • By default child threads value is exactly same as parent Threads value but we can provide customized value for child Thread by overriding ChildValueMethod.
  • InheritableThreadLocal is the child class of ThreadLocal and Hence all methods present in thread local by default available to InheritableThreadLocal.
  • In Addition to this method it contains only one method
    public Object childValue(Object parentValue)
public class InheritableThreadLocalDemo1 {
	public static void main(String[] args) {
		new ParentThread().start();
	}
}
class ParentThread extends Thread{
	static ThreadLocal tl = new ThreadLocal();
	static InheritableThreadLocal itl = new InheritableThreadLocal();
	public void run() {
		tl.set("Parent Value");
		itl.set("Parent Value");
		new ChildThread().start();
	}
}
class ChildThread extends Thread{
	public void run() {
		System.out.println("Thread Local Value : "+ParentThread.tl.get());
		System.out.println("Inheritable Thread Local Value : "+ParentThread.itl.get());
	}
}

Callable and Future

  • In the case of runnable job Thread won’t return anything after completing the job.
  • If a thread is required to return some result after execution then we should go for callable.
  • Callable interface contains only one method call()
    public Object call() throws Exception.
  • If we submit a callable object to executor then after completing the job thread returns an Object of type Future i.e Future object can be used to retrieve the result from callable job.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class CallableDemo {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		MyCallable[] jobArray = { new MyCallable(10),new MyCallable(20),new MyCallable(30),new MyCallable(40),new MyCallable(50)};
		ExecutorService service = Executors.newFixedThreadPool(3);
		for(MyCallable job : jobArray) {
			Future f = service.submit(job);
			System.out.println(f.get());
		}
		service.shutdown();
	}
}
class MyCallable implements Callable{

	int number;
	MyCallable(int number){
		this.number = number;
	}
	
	public Object call() throws Exception {
		System.out.println("Thread execution started for "+number+" Thread Name : "+Thread.currentThread().getName());
		int total=0;
		for(int i=0;i<=number;i++) {
			total = total + i;
		}
		return total;
	}
}

Difference between Runnable and Callable

RunnableCallable
1. If a thread is not required to return anything after completing the job then we should use Runnable1. If a thread is required to return something after completing the job then we should go for callable.
2. Runnable interface contains only one method run()2. Callable interface contains only one method call()
3. Runnable job not required to return anything and hence return type of run method is void3. Callable job is required to return something and hence return type of call method is object.
4. Within the run method if there is any chance of raising checked exception we need to handle it with try catch because we can’t use throws keyword in run method4.With in call method if there is any chance of raising checked exception we are not required to handle it using try catch because call method already throws exception.
5. Runnable interface present in java.lang package5. Callable interface present in java.util.concurrent package
6. Introduced in java 1.0 version6. Introduced in java 1.5 version