Question: I have a script that runs every 15 seconds and it creates a new thread every time. Now and then this thread gets stuck and blockes the synchronisation process of my program. Now i want to kill the process with a specific name if the thread is stuck for 20 loops. Here is my code:

```
public void actionPerformed(ActionEvent e) {

        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss");
        System.out.println(ft.format(dNow));

        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        boolean loopthreadfound = false;
        for (int i = 0; i < noThreads; i++){
        try{    
        if (lstThreads[i].getName() == "loopthread"){loopthreadfound = true;}           
        }catch(Exception e1){System.out.println(e1);}
        }

        if (loopthreadfound == false){
            loopthreadcounter = 0;
            //Starten in nieuwe thread
            Thread loopthread = new Thread() {

            public void run() {

                try {
                    checkonoffline();
                    checkDBupdates();
                    } catch (JSONException | SQLException | IOException e1) {
                    System.out.println(e1);
                    }
            }

            };

            loopthread.setName("loopthread");
            loopthread.start();

        }else{

            loopthreadcounter++;
            System.out.println("Loopthread already running...   counter: " + loopthreadcounter);        

            if (loopthreadcounter > 20){

             // HERE I WANT TO KILL THE THREAD "loopthread "

            }
        }

}

```

Answer: 
Ok, after a bit of work I believe I have a solution.

As part of my CI (Continuous Integration) job I run:

```
cdk synth

```

I then save the contents of the cdk.out folder to a repository (I'm using Octopus Deployment).

As part of my CD (Continuous Deployment) job I have the following (Powershell):

```
$Env:AWS_ACCESS_KEY_ID={your key}
$Env:AWS_SECRET_ACCESS_KEY={your secret}
$Env:AWS_DEFAULT_REGION={your region}

$cdk=[Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData) + "\npm\cdk.cmd"
& $cdk --app . deploy {your stack name} --require-approval never

```

So the "cdk synth" will generate the template and assets required for deployment.

The "cdk --app . depoy {your stack name} --require-approval never" is telling aws-cdk to us the existing templates and assets. This avoids a situation where the CD process may produce a different setup than the CI process. The "." indicates that the templates & assets are in the current folder.

You will need to install node & aws-cdk on the CD server (in my case an Octopus Deploy tentacle);

Node install is easy, just log in and install.

To add aws-cdk perform the following (using an administrator powershell):

```
npm prefix -g  // Make note of the path
npm config set prefix C:\Windows\System32\config\systemprofile\AppData\Roaming\npm
npm install -g aws-cdk
npm config set prefix {original path}

```

Note that the npm path maybe different for your usage - will depend on the user account used for the CD process and Windows version.
One option is to generate a CloudFormation ChangeSet using CDK and then deploy that ChangeSet later. To create the ChangeSet run `cdk deploy <StackName> --execute false`. This will synthesize the stack as well as upload templates and assets to S3. The ChangeSet can then be executed at anytime without CDK.