Skip to content

Instantly share code, notes, and snippets.

How to kill Thread?

Thread.stop()

Thread.stop() is deemed as deprecated. Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged.

public class Test implements Runnable {

    private String name;

 public Test(String name) {

Lambda expressions

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance in multicore environments.

Example of lambda

     import java.util.ArrayList;
     import java.util.List;

     public class Lambda {
public static void main(String[] args) {
@darsen44
darsen44 / Calculator.md
Last active April 19, 2018 07:21
Simple calculator
import java.awt.dnd.InvalidDnDOperationException;
import java.util.HashMap;
import java.util.Map;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Calculator {

    private static final char ADD = '+';
package Comparators;

   public class Person implements Comparable<Person> {

    private int age;
    private String name;

     public Person(int age, String name) {
         this.age = age;

this.name = name;

Interface NavigableMap

Type Parameters:

K - the type of keys maintained by this map

V - the type of mapped values

Superinterfaces:

Map<K,V>, SortedMap<K,V>

All Known Subinterfaces:

package Lists;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListTiming {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList =  new ArrayList<>();

Queue

A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out).

Queue methods

boolean add(E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

boolean offer(E e)

>Inserts the specified element into this queue if it i

Implementation of List

LinkedList

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null). All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

ArrayList

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

AbstractList

>This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential acce

@darsen44
darsen44 / CkeckNumber.java
Last active March 20, 2018 16:35
CheckNumber 1.0
package cursor.hw;
import java.io.*;
import java.util.Scanner;
public class CheckNumber {
public static void main(String[] args) throws IOException {
System.out.println(" Welcome to CheckNumber program");
boolean addExit = false;
@darsen44
darsen44 / for.md
Last active March 19, 2018 21:20
"For" different realization

Loop "FOR"

"for" is very common loop in java.

Structure "for":

    for(initialization, logical expression (condition), step (iteration)){
         commands
    }

1. Standart use of "for":