NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.standardchartered.s2b.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.log4testng.Logger;

import Report.ReportFactory;

import com.google.common.base.Optional;
import com.standardchartered.s2b.excelAPI.ExcelOperation;
import com.standardchartered.s2b.utility.Constant;

public class Multithreading {


//global variable.
private ExecutorService executorPool;
public WebDriver driver;


public static void main(String args[])
{
Multithreading multithreading = new Multithreading();

// multithreading.startMultithreading();

}


public void startMultithreading(){

final ExcelOperation excelOperation = new ExcelOperation();
try {
DriverScript driverscript = new DriverScript();
excelOperation.readExecutionVariables();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



/********** STAGE 1: FIND NO OF THREADS TO BE INITIATED ***********************/
Map<String, Integer> columnName= new HashMap<String, Integer>();
Set<String> journeyIDList = new LinkedHashSet<String>();

journeyIDList = excelOperation.readThreadCount();
System.out.println("Number of Thread to be initiated - " + journeyIDList.size());


/************STAGE 2: Thread Pool Executor ********/

executorPool = Executors.newFixedThreadPool(journeyIDList.size());



/****************** IMPLEMENTATION*************/
final Iterator<String> iterator = journeyIDList.iterator();
while(iterator.hasNext())
{
executorPool.submit(new Runnable(){

@Override
public void run() {

//Not Mandatory print statements
String command = iterator.next();
System.out.println("Thread started : " + Thread.currentThread().getName());
Thread.currentThread().setName(command);
System.out.println("Thread proceeding with new name -> " +Thread.currentThread().getName()+" Start. Command = "+command);
WebDriver driver = WebdriverFactory.getInstance().getDriver();
System.out.println("01. WebDriver instance Created | Before executeTestSuite() function " + command);
excelOperation.executeTestsuite(Thread.currentThread().getName());

/************End of Method ****/

System.out.println("Thread Ended : " + Thread.currentThread().getName());

}}
);
}

/****** Awaiting Termination **************/

System.out.println("Termination Initiated by -> " + Thread.currentThread().getName());
executorPool.shutdown();
while (!executorPool.isTerminated())
{
try{
Thread.sleep(1000);
}catch (InterruptedException e)
{
e.printStackTrace();
System.out.println("Thread interrupted -> ");
Thread.currentThread().interrupt();
}
}

/**************** Adding ShutDown Hook *******************************/
System.out.println("Is it executed from eclipse ? " + System.getenv("RUNNING_IN_ECLIPSE"));


Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run() {
System.out.println("Shutting down executor using shutDownHook");

try {
if (!executorPool.awaitTermination(30, TimeUnit.SECONDS)) {
System.out.println("Executor yet to terminate after waiting time...");
exitMethod();
List<Runnable> notExecuted= executorPool.shutdownNow();
System.out.println("List of dropped task with size " + notExecuted.size());
}
else{
System.out.println("Executor Pool Terminated -> " + executorPool.isTerminated());
}
}catch(InterruptedException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
});

/*******************************************************************/
//CloseReport
exitMethod();

}


public void exitMethod()
{
System.out.println("Generating Report : -> " +Thread.currentThread().getName());
ReportFactory.closeReporter();
System.out.println("Report Generated ");

System.out.println("Closing Driver");
WebdriverFactory.getInstance().removeDriver();

try {
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.