tutorial

Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Thread pool in Java


The Thread pool

Introduction

  • Executor implementations use thread pools.
  • The thread pool is a collection of worker threads that are ready to serve.
  • Creating new threads and manage them uses a big amount of data.
  • Worker threads in the thread pool will help to reduce this overhead of creating threads.
  • Tasks are submitted to the pool via an internal queue called blocking queue.
  • If there are more tasks than active threads, these tasks will be inserted into a blocking queue until a thread becomes available.
  • A common type of thread pool is the fixed thread pool.

Fixed thread pool

  • This has a specific number of running threads.
  • If a thread stops, this will replace with a new thread.
  • The main advantage of the fixed thread pool is it will limit the threads of an application.
  • It won't let the application exceed the limit of threads that the application handles.

Java ExecutorService


Java ExecutorService

Introduction

  • If an application has few threads, it can be used threads very easily using the above methods.
  • But if an application has many threads, it will be not easy to handle this.
  • Executors can be used to avoid this complexity.
  • The executor framework is a framework that can be used for creating, managing and executing threads.
  • This provides an asynchronous feature to Java applications.

Features of Executor service

  • Thread creation
    • Provides thread pool and various methods to create threads.
  • Thread Management
    • The thread pool will manage the life cycle of threads.
  • Thread Execution
    • Various methods will be provided
    • Thread schedulers can be used.

Executor interfaces in the Java Concurrency API

  • Executor
    • Provides a single method execute() to create a thread.
    • (new Thread(r)).start() can be replaced with e.execute(r)
  • ExecutorService
    • Also provides execute() method but this accepts both Runnable and Callable objects.
  • ScheduledExecutorService
    • This can be used to act as the asynchronous manner in Java
    • It can be used periodically and after a specific timeout.
    • Runnable and Callable tasks can be executed


Thread priority


Thread priority

  • Each thread has a priority.
  • It starts from 1 to 10 and the highest priority is 10 while the lowest is 1.
  • The default priority of the main thread is 5.
  • If you set the thread priority out of 10, it will give a compile-time error
Exception in thread "main" java.lang.IllegalArgumentException

List vs Queue vs Set vs Map


 List vs Queue vs Set vs Map


ListQueueSetMap
DuplicatesYesYesNoNo(Allow duplicate values not keys)
OrderYesYesNoNo
Null valuesYesPriority queue doesn't allow, but queue using LinkedList allows nullSingle nullSingle null key and many null values

What is REST


REST

Introduction

  • Stands for Representational State Transfer.
  • REST is a web standards-based architecture that uses the HTTP protocol (port 80) for data communication.
  • Uses HTTP methods for data communication 
  • REST server simply provides access to resources and client access and presents the resource.
  • REST is stateless, so there is no session.
  • REST uses various representations like TXT, JSON, XML...etc to represent resources.

Methods in REST

  • POST
    • Sends data to the server for creating a new resource, maybe an entity. Often used when uploading a file or submitting a web form.
  • GET 
    • Retrieves data from the server. Should have no other effect.
  • PUT 
    • Similar to POST, but used to replace an existing entity.
  • PATCH
    • Similar to PUT, but used to update only certain fields within an existing entity.
  • DELETE 
    • Removes data from the server.
  • TRACE 
    • Provides a way to test what the server receives. It simply returns what was sent.
  • OPTIONS 
    • This allows a client to get information about the request methods supported by a service. 
    • The relevant response header is Allow with supported methods.
  • HEAD 
    • This returns only the response headers.
  • CONNECT 
    • Used by browser when it knows it talks to a proxy and the final URI begins with https://. 
    • The intent of CONNECT is to allow end-to-end encrypted TLS sessions, so the data is unreadable to a proxy.

Directives in Angular 11


 Directives

  • Directives are instructions in the DOM
  • It adds additional behaviors to the elements
  • Using inbuilt directives we can manage forms, lists, styles, and what users see.
  • There are a few types
    • Component directives
    • Attribute directives
    • Structural directives

Component directives

  • These directives are used in the main class.
  • Declared by @Component
Ex:
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

Attribute directives

  • Listen and modify the behavior of other HTML elements, attributes, properties, and components
    • NgClass
      • This allows to set the CSS classes dynamically.
    • NgStyles
      • This directive lets you to set a given DOM element's style properties.
<div [ngStyle]="{'background-color':'green'}"></<div>
      • Instead of this you can use following too.
[style.color]="getColor(person.country)"
    • NgModel
      • This bind inputs, select, text areas...etc and store the required user value in a variable.
      • Add two-way data binding
  • This allows to create new directives too.

Structural directives

  • These directives manipulate the DOM element or change the structure of the DOM element
  • These directives begin with *
  • Ex: *ngIf, *ngFor
<ul *ngFor="let age of ages">
    <li *ngIf="age > 20">
        {{age}}
    </li>
</ul>




Access token vs refresh token


Access token vs refresh token

Access token

  • This is short-lived
  • Send API request, with the access token
  • If the access token is invalid, fail and ask the user to re-authenticate
  • There are few types of access tokens
    • Bearer tokens
    • JWT tokens
    • Opaque token

Refresh token

  • These tokens are long-lived
  • Refresh tokens are used to retrieve access tokens
  • If the access token is invalid, try to update it using the refresh token
  • If the refresh request passes, update the access token and re-send the initial API request
  • If the refresh request fails, ask the user to re-authenticate

Throw vs throws in exception handling


Throw vs throws



Throw
Throws
Uses inside a method when it is required to throw an exceptionThis is used in the method signature in which methods that exceptions can occur. 
Only one exception can be thrownThrows can be used to declare multiple exceptions
Used to handle unchecked exceptionsUsed to handle checked exceptions
Usually used to handle custom exceptionsThrows means, it says this method can be thrown these exceptions, when you use this method, you need to handle it.