Tuesday, 23 July 2019

How to kill a thread in JAVA ??

Ans:

'Killing a thread' is not the right phrase to use. Here is one way we can implement graceful exit of the thread:
class TaskThread implements Runnable {

    boolean shouldStop;

    public TaskThread(boolean shouldStop) {
        this.shouldStop = shouldStop;
    }

    @Override
    public void run() {

        System.out.println("Thread has started");

        while (!shouldStop) {
            // do something
        }

        System.out.println("Thread has ended");

    }
public void stop() {
        shouldStop = true;
    }


}
public class ThreadStop
{
public static void main(String[] args)
{
System.out.println("Start");

// Start the thread
TaskThread task = new TaskThread(false);
Thread t = new Thread(task);
t.start();

  // Stop the thread
task.stop();

  System.out.println("End");

}
}

Friday, 5 July 2019

JAVA StringBuffer and StringBuilder object conversion to String


We can convert a StringBuffer or StringBuilder object  to String by using toString() method of Object class.
StringConversion.java
class StringConversion
{
          public static void main(String[] args)
          {
                   StringBuffer sb1=new StringBuffer("Silan");
                   StringBuilder sb2=new StringBuilder("Technology");
                   String s1=sb1.toString();
                   String s2=sb2.toString();
                   System.out.println(s1+" "+s2);
          }
}
Output:
Silan Technology


StringConversion1.java
class StringConversion1
{
          public static void main(String[] args)
          {
                   StringBuffer sb1=new StringBuffer("Silan");
                   StringBuffer sb2=new StringBuffer("Silan");
                   String s1=sb1.toString();
                   String s2=sb2.toString();
                   if(s1.equals(s2))
                   {
                             System.out.println("s1 and s2 contents are equal");
                   }
                   else
                   {
                             System.out.println("s1 and s2 contents are not equal");
                   }
          }
}
Output:
s1 and s2 contents are equal

Saturday, 9 February 2019

JAVA : How to generate OTP


OtpExample.java
package java8s;

import java.util.*;

public class OtpExample {
    
     static char[] show(int len)
    { 
        System.out.print("Your OTP is : ");
 
        String numbers = "0123456789";
 
        Random rd = new Random();
 
        char[] otp = new char[len];
 
        for (int i = 0; i < len; i++)
        {
            
            otp[i] =
             numbers.charAt(rd.nextInt(numbers.length()));
        }
        return otp;
    }

     public static void main(String[] args) {
          
           int length = 5;
        System.out.println(show(length));

     }

}

Output:
Your OTP is : 35838

Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9658317757

JAVA : How to generate password


//JAVA source code to generate random password
PasswordExample.java
package java8s;

import java.util.*;
public class PasswordExample {

     public static void main(String[] args) {
          
           int length = 10;
        System.out.println(silanPassword(length));
          

     }
    
     static char[] silanPassword(int len)
    {
        System.out.print("Your new password is : ");
 
       
        String uchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lchars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*_=+-/.?<>)";
 
 
        String values = uchars + lchars +
                        numbers + symbols;
 
        
        Random rd = new Random();
 
        char[] password = new char[len];
 
        for (int i = 0; i < len; i++)
        {
           
            password[i] =
              values.charAt(rd.nextInt(values.length()));
 
        }
        return password;
    }

}
Output:
Your new password is : mR!XPdfVmg

Note : Here we have used random() to generate the password. It may change every time due to use of random() method.

Thanks & Regards:'
Trilochan Tarai
SILAN Technology & java8s.com
0674-2361252

Monday, 17 December 2018

JAVA ArrayList Constructors

JAVA ArrayList Constructors


                 java.util.AbstractList
                          java.util.ArrayList

public class ArrayList
extends AbstractList
1.    ArrayList() : Create an empty ArrayList object with default initial capacity 10. Once ArrayList reaches its max capacity then new ArrayList will be created with a new capacity.
new capacity=(current capacity*3/2)+1
i.e (10*3/2)+1=16 
after this new ArrayList is created the previous one will be deleted and it will be treated as garbage.

2.    ArrayList(int initialcapacity) : Constructs an empty ArrayList with the specified initial capacity given by the user.
3.    ArrayList(Collection c) : For every Collection object, we can create an equivalent ArrayList.
ArrayListExample1.java
importjava.util.*;
classArrayListExample1
{
    public static void main(String[] args)
    {
            ArrayList al=new ArrayList();
            al.add("Sashi");
            al.add("Satyajit");
            al.add(20);
            al.add(null);
            System.out.println(al);          //[Sashi,Satyajit,20,null]
            al.remove(1);
           al.add(1,"Sasmita");
           al.add("Nilan");
           System.out.println(al);           //[Sashi,Sasmita,20,null,Nilan]
      }
}
Output
[Sashi,Satyajit,20,null]
[Sashi,Sasmita,20,null,Nilan]


Thanks & Regards:
Trilochan Tarai
SILAN Technology
java8s.com
0674-2361252

Wednesday, 7 November 2018

Difference between JAVA Collection & Collections


Collection is a root level interface of the Java Collection Framework. Most of the classes in Java Collection Framework implements from this interface. List, Set and Queue are main sub interfaces of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect implementations of Collection interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package.
Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. For example, it has the method to find the maximum element in a collection, it has the method to sort the collection, it has the method to search for a particular element in a collection. Below is the list of some important methods of Collections class.
Collections.max()
Collections.min()
Collections.sort()
Collections.synchronizedCollection()



Thanks & Regards:
Trilochan Tarai
9439202111

Thursday, 11 October 2018

JAVA 11 Features

Some JAVA 11 Features


1. Nest-Based Access Control
2. Dynamic Class-File Constants
3. Epsilon: A No-Op Garbage Collector
4. Remove the JAVA EE and CORBA Modules
5. HTTP Client(Standard)
6. Local-Variable Syntax for Lambda Parameters
7. Unicode 10
8. Flight Recorder


Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9439202111
0674-2361252