Skip to main content

Posts

Showing posts from December, 2020

What is a Livelock scenario in java?

Image by Clker-Free-Vector-Images from Pixabay Livelock is a scenario in which two or more block each other by responding to an action caused by another thread. In a deadlock situation, two or more threads wait in one specific state. In a Livelock scenario, two more threads change their state in such a way that it prevents progress on their regular work. E.g. Consider a scenario in which two threads try to acquire two locks.  They release a lock that they have acquired when they cannot acquire the second lock. In a Livelock situation, both threads concurrently try to acquire the locks.  Only one thread would succeed, the second thread may succeed in acquiring the second lock. Now both threads hold two different locks. And both threads want to have both locks.  So they release their lock and try again from the beginning. This situation keeps repeating multiple times.

Recursion-maze problem - Rat in the Maze - Game

  package com.bei.Demo01_recursion; public class MiGong {     public static void main(String[] args)  {         //First create a two-dimensional array to simulate the maze         int [][]map=new int[8][7];         //Use 1 for wall         for (int i = 0; i <7 ; i++) {             map[0][i]=1;             map[7][i]=1;         }         for (int i = 0; i <8 ; i++) {             map[i][0]=1;             map[i][6]=1;         }         //Set the bezel         map[3][1]=1;         map[3][2]=1;         //Output         for (int i = 0; i <8 ; i++) {             for (int j = 0; j < 7; j++) {                 System.out.print(map[i][j]+" ");             }             System.out.println();         }         System.out.println("----------------------------");         setWay(map,1,1);         for (int i = 0; i <8 ; i++) {             for (int j = 0; j < 7; j++) {                 System.out.print(map[i][j]+" ");             }             System.out.pri

Using java to for file copy function interface

 File copy program Purpose: 1. 1. Familiar with the writing of Swing interface; 2.2. Learn to use the JFileChooser file selector; 3.Learn to use file input stream FileInputStream and output stream FileOutputStream to copy files. Content: Use Swing basic components and a suitable layout manager to write the file copy program interface, use the JFileChooser file selector to realize the graphical operation of the source file and the target path, the specific operation flow of the file copy program is as follows: (1) The main interface of the program operation (2) Click the "Browse" button to pop up the file selection interface, select the source file to be copied (3) In the same way, select the target path, and then click "Start Copy" to complete the file copy Additional function: On the basis of the completion of the above functions, the file copy progress bar function can be added. code show as below: import java.awt.Color; import java.awt.EventQueue; import java.awt

What is a Deadlock situation in Java? What are the minimum requirements for a Deadlock situation in a program in Java? How can we prevent a Deadlock in Java?

  Photo by  ThisIsEngineering  from  Pexels What is a Deadlock situation in Java?  A Deadlock is a situation in which two or more threads are waiting on each other to release a resource. Each thread is waiting for a resource that is held by the other waiting thread. At times there is a circular wait when more than two threads are waiting on each other’s resources. What are the minimum requirements for a Deadlock situation in a program in Java? For a deadlock to occur following are the minimum requirements: 1. Mutual exclusion: There has to be a resource that can be accessed by only one thread at any point of time. 2. Resource holding: One thread locks one resource and holds it, and at the same time it tries to acquire lock on another mutually exclusive resource. 3. No preemption: There is no pre-emption mechanism by which resource held by a thread can be freed after a specific period of time. 4. Circular wait: There can be a scenario in which two or more threads lock one resource each

Use Java to write a simple solar calendar

Photo by  Ketut Subiyanto  from  Pexels   The difficulty of printing a month’s calendar is to find out the number of weeks corresponding to the first day of the month. A good way to solve this problem is to find out that the first day of January of the year corresponds to Monday, so that the number of weeks on the first day of the month required = (the total number of days between the year and the year entered + the number of days lost The total number of days between January of the year and the month entered + 1)%7. Then the year I was looking for was 1900.  import java.util.Scanner; public class Calendar {     public static void main(String[] args) {         //Enter the year and month         //Calculate the total number of days between 1900 and the year entered         //Judging the average leap year         Scanner scanner=new Scanner(System.in);         System.out.println("Enter a year:");         int year=scanner.nextInt();         int yearTotalDays = 0;         for(int

Understanding Java Virtual Machine ( JVM )

 Detailed HotSpot virtual machine Java object creation process Photo by Michiel Leunens on Unsplash (1) When the virtual machine encounters a new instruction, it first checks whether the parameters of this instruction can locate a symbol reference of a class in the constant pool, and check whether the class represented by this symbol reference has been loaded, connected and initialized. If not, execute the loading process of the class. (2) Allocate memory for the object. A. Assuming that the Java heap is regular, all used memory is placed on one side, free memory is placed on the other side, and a pointer is placed in the middle as an indicator of the dividing point. Allocating memory just moves the pointer to the free space by the same distance as the size of the object. This allocation is called "pointer collision". B. Assuming that the Java heap is not regular, and the used memory and the free memory are interlaced, then There is no way to "pointer collision". T

Bubble sort operation of java program

 Today, I let's learn about the principle of bubble sorting in Java. Photo by  ThisIsEngineering  from  Pexels public class Test05 {     public static void main(String[] args) {     int[] arr = new int[]{23,61,45,12,53,126}; //Define array     for (int i = 0; i < arr.length-1; i++) {         for (int j = 0; j < arr.length-1-i; j++) {             if (arr[j]>arr[j+1]){                 int temp = arr[j];                 arr[j] = arr[j+1];                 arr[j+1] = temp;             }         }     }     for (int i = 0; i < arr.length; i++) { //Output sorted array         System.out.println(arr[i]);     }     } } 1. Java's bubble sort is a simple operation for array traversal. 2. My array has six elements, so it means I have to loop five times to select the first to fifth largest number, so the sixth is naturally The smallest, so "arr.length" should be reduced by one or not, but the operating efficiency will be a bit slower. 3. The essence of bubble sorti