Question stringlengths 1 113 | Answer stringlengths 22 6.98k |
|---|---|
What does Java Inner class need? | Sometimes users need to program a class in such a way so that no other class can access it. Therefore, it would be better if you include it within other classes.
If all the class objects are a part of the outer object then it is easier to nest that class inside the outer class. That way all the outer class can acces... |
What is the Difference between nested class and inner class in Java? | An inner class is a part of a nested class. Non-static nested classes are known as inner classes. |
Can you give me the Types of Nested classes? | There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.
o Non-static nested class (inner class)
1. Member inner class
2. Anonymous inner class
3. Local inner class
o Static nested class
Type: Member inner class
Description: : A ... |
What is Java Member Inner class? | A non-static class that is created inside a class but outside a method is called member inner class. It is also known as a regular inner class. It can be declared with access modifiers like public, default, private, and protected.
Syntax:
class Outer{
//code
class Inner{
//code
}
} |
Can you give me an example of Java Member Inner Class? | In this example, we are creating a msg() method in the member inner class that is accessing the private data member of the outer class.
TestMemberOuter1.java
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(St... |
How to instantiate Member Inner class in Java? | An object or instance of a member's inner class always exists within an object of its outer class. The new operator is used to create the object of member inner class with slightly different syntax.
The general form of syntax to create an object of the member inner class is as follows:
Syntax:
OuterClassRefere... |
What is the Internal working of Java member inner class? | The java compiler creates two class files in the case of the inner class. The class file name of the inner class is "Outer$Inner". If you want to instantiate the inner class, you must have to create the instance of the outer class. In such a case, an instance of inner class is created inside the instance of the outer c... |
What is the Internal code generated by the compiler in Java? | The Java compiler creates a class file named Outer$Inner in this case. The Member inner class has the reference of Outer class that is why it can access all the data members of Outer class including private.
import java.io.PrintStream;
class Outer$Inner
{
final Outer this$0;
Outer$Inner()
... |
What is Java Anonymous inner class? | Java anonymous inner class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as overloading methods of a class or interface, without having to actually subclass a class.
In simple word... |
Can you provide a Java anonymous inner class example using class? | TestAnonymousInner.java
abstract class Person{
abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Output:
nice fruits |
Can you provide a Java anonymous inner class example using interface? | interface Eatable{
void eat();
}
class TestAnnonymousInner1{
public static void main(String args[]){
Eatable e=new Eatable(){
public void eat(){System.out.println("nice fruits");}
};
e.eat();
}
}
Output:
nice fruits |
What is Java Local inner class? | A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop, or an if clause. Local Inner classes are not a member of any enclosing classes. They belo... |
Can you provide a Java local inner class example? | LocalInner1.java
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.di... |
What is Internal class generated by the compiler? | In such a case, the compiler creates a class named Simple$1Local that has the reference of the outer class.
import java.io.PrintStream;
class localInner1$Local
{
final localInner1 this$0;
localInner1$Local()
{
super();
this$0 = Simple.this;
}
void msg... |
What is the Rules for Java Local Inner class? | Rules for Java Local Inner class
1) Local inner class cannot be invoked from outside the method.
2) Local inner class cannot access non-final local variable till JDK 1.7. Since JDK 1.8, it is possible to access the non-final local variable in the local inner class. |
Can you give me an Example of local inner class with local variable? | LocalInner2.java
class localInner2{
private int data=30;//instance variable
void display(){
int value=50;//local variable must be final till jdk 1.7 only
class Local{
void msg(){System.out.println(value);}
}
Local l=new Local();
l.msg();
}
public static void main(String... |
What is Java static nested class? | A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.
-It can access static data members of the outer class, including private.
-The static nested class cannot access non-static... |
Can you give me a Java static nested class example with instance method? | TestOuter1.java
class TestOuter1{
static int data=30;
static class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
Output:
data is 30 |
Can you give me a Java static nested class example with a static method | If you have the static member inside the static nested class, you don't need to create an instance of the static nested class.
TestOuter2.java
public class TestOuter2{
static int data=30;
static class Inner{
static void msg(){System.out.println("data is "+data);}
}
public static void main(String ... |
What is Java Nested Interface? | An interface, i.e., declared within another interface or class, is known as a nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred to by the outer interface or class. It can't be accessed directly. |
What are the Points to remember for nested interfaces? | There are given some points that should be remembered by the java programmer.
-The nested interface must be public if it is declared inside the interface, but it can have any access modifier if declared within the class.
-Nested interfaces are declared static |
What is the Syntax of nested interface which is declared within the interface? | interface interface_name{
...
interface nested_interface_name{
...
}
} |
What is the Syntax of nested interface which is declared within the class? | class class_name{
...
interface nested_interface_name{
...
}
} |
Can you give me an Example of nested interface which is declared within the interface? | In this example, we will learn how to declare the nested interface and how we can access it.
TestNestedInterface1.java
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("He... |
Can we define a class inside the interface? | Yes, if we define a class inside the interface, the Java compiler creates a static nested class. Let's see how can we define a class within the interface:
interface M{
class A{}
} |
What is Multithreading in Java? | Multithreading in Java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
However, we use multithreading than multiprocessing because threads use a shared memo... |
What is the Advantages of Java Multithreading? | 1) It doesn't block the user because threads are independent and you can perform multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread. |
What is Thread in java? | A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution.
Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It uses a shared memory area.
Java Multithreading
As shown in the above figure, a thread is executed inside... |
What is Java Thread class? | Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. |
What are the Java Thread Methods? | Java Thread class
Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.
Java Thread Methods
Modifier and Type: void
Method: start()
Description: It i... |
What is the Life cycle of a Thread (Thread States)? | In Java, a thread always exists in any one of the following states. These states are:
1.New
2.Active
3.Blocked / Waiting
4.Timed Waiting
5.Terminated |
Can you give me an Explanation of Different Thread States? | New: Whenever a new thread is created, it is always in the new state. For a thread in the new state, the code has not been run yet and thus has not begun its execution.
Active: When a thread invokes the start() method, it moves from the new state to the active state. The active state contains two states within it: one... |
What are the Implementation of Thread States? | In Java, one can get the current state of a thread using the Thread.getState() method. The java.lang.Thread.State class of Java provides the constants ENUM to represent the state of a thread. These constants are:
public static final Thread.State NEW
It represents the first state of a thread that is the NEW state.... |
Can you give me an example of Java Program for Demonstrating Thread States? | The following Java program shows some of the states of a thread defined above.
FileName: ThreadState.java
// ABC class implements the interface Runnable
class ABC implements Runnable
{
public void run()
{
// try-catch block
try
{
// moving thread t2 to the state timed waiting
Thread.sleep(100); ... |
Java Threads | How to create a thread in Java? | There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
|
What are the Commonly used Constructors of Thread class? | Commonly used Constructors of Thread class:
-Thread()
-Thread(String name)
-Thread(Runnable r)
-Thread(Runnable r,String name) |
What are the Commonly used methods of Thread class? | 1.public void run(): is used to perform action for a thread.
2.public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
3.public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
4... |
What is Runnable interface? | The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().
1.public void run(): is used to perform action for a thread. |
How to Start a thread in Java? | The start() method of Thread class is used to start a newly created thread. It performs the following tasks:
-A new thread starts(with new callstack).
-The thread moves from New state to the Runnable state.
-When the thread gets a chance to execute, its target run() method will run. |
Can you give me a Java Thread Example by extending Thread class? | FileName: Multi.java
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output:
thread is running... |
Can you give me a Java Thread Example by implementing Runnable interface? | FileName: Multi3.java
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
Output:
... |
How to use the Thread Class: Thread(String Name)? | We can directly use the Thread class to spawn new threads using the constructors defined above.
FileName: MyThread1.java
public class MyThread1
{
// Main method
public static void main(String argvs[])
{
// creating an object of the Thread class using the constructor Thread(String name)
Thread t... |
How to use the Thread Class: Thread(Runnable r, String name)? | Observe the following program.
FileName: MyThread2.java
public class MyThread2 implements Runnable
{
public void run()
{
System.out.println("Now the thread is running ...");
}
// main method
public static void main(String argvs[])
{
// creating an object of the class MyTh... |
What is Thread Scheduler in Java? | A component of Java that decides which thread to run or execute and which thread to wait is called a thread scheduler in Java. In Java, a thread is only chosen by a thread scheduler if it is in the runnable state. However, if there is more than one thread in the runnable state, it is up to the thread scheduler to pick ... |
What is the Working of the Java Thread Scheduler? | Let's understand the working of the Java thread scheduler. Suppose, there are five threads that have different arrival times and different priorities. Now, it is the responsibility of the thread scheduler to decide which thread will get the CPU first.
The thread scheduler selects the thread that has the highest prio... |
What is Thread.sleep() in Java? | The Java Thread class provides the two variant of the sleep() method. First one accepts only an arguments, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known ... |
What is The sleep() Method Syntax in Java? | Following are the syntax of the sleep() method.
public static void sleep(long mls) throws InterruptedException
public static void sleep(long mls, int n) throws InterruptedException
The method sleep() with the one parameter is the native method, and the implementation of the native method is accomplished in a... |
What are the Important Points to Remember About the Sleep() Method? | Whenever the Thread.sleep() methods execute, it always halts the execution of the current thread.
Whenever another thread does interruption while the current thread is already in the sleep mode, then the InterruptedException is thrown.
If the system that is executing the threads is busy, then the actual sleeping ... |
Can you give me an Example of the sleep() method in Java : on the custom thread? | e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start();
}
}
Output:
1
1
2
2
3
3
4
... |
Can you give me an Example of the sleep() Method in Java : on the main thread? | FileName: TestSleepMethod2.java
// important import statements
import java.lang.Thread;
import java.io.*;
public class TestSleepMethod2
{
// main method
public static void main(String argvs[])
{
try {
for (int j = 0; j < 5; j++)
{
// The main thread sleeps for the... |
Can you give me an Example of the sleep() Method in Java: When the sleeping time is -ive? | The following example throws the exception IllegalArguementException when the time for sleeping is negative.
FileName: TestSleepMethod3.java
// important import statements
import java.lang.Thread;
import java.io.*;
public class TestSleepMethod3
{
// main method
public static void main(String... |
Can we start a thread twice? | No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.
Let's understand it by the example given below:
public class TestThreadTwice1 extends Thread{
public void run()... |
What if we call Java run() method directly instead start() method? | Each thread starts in a separate call stack.
Invoking the run() method from the main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
FileName: TestCallRun1.java
class TestCallRun1 extends Thread{
public void run(){
System.out.println("running...... |
What is Java join() method? | The join() method in Java is provided by the java.lang.Thread class that permits one thread to wait until the other thread to finish its execution. Suppose th be the object the class Thread whose thread is doing its execution currently, then the th.join(); statement ensures that th is finished before the program does t... |
Can you give me the Description of The Overloaded join() Method? | join(): When the join() method is invoked, the current thread stops its execution and the thread goes into the wait state. The current thread remains in the wait state until the thread on which the join() method is invoked has achieved its dead state. If interruption of the thread occurs, then it throws the Interrupted... |
Can you give me an Example of join() Method in Java? | The following program shows the usage of the join() method.
FileName: ThreadJoinExample.java
// A Java program for understanding
// the joining of threads
// import statement
import java.io.*;
// The ThreadJoin class is the child class of the class Thread
class ThreadJoin extends Thread
{
// over... |
What is The Join() Method: InterruptedException? | We have learnt in the description of the join() method that whenever the interruption of the thread occurs, it leads to the throwing of InterruptedException. The following example shows the same.
FileName: ThreadJoinExample1.java
class ABC extends Thread
{
Thread threadToInterrupt;
// overriding the run() metho... |
Can you give me Some More Examples of the join() Method? | Let' see some other examples.
Filename: TestJoinMethod1.java
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static v... |
Can you give me a join(long miliseconds) Method Example? | Filename: TestJoinMethod2.jav
class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Tes... |
How to Name a Thread? | The Thread class provides methods to change and get the name of a thread. By default, each thread has a name, i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using the setName() method. The syntax of setName() and getName() methods are given below:
public String getName(): is used to re... |
Can you give me an Example of naming a thread : Using setName() Method? | FileName: TestMultiNaming1.java
class TestMultiNaming1 extends Thread{
public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestMultiNaming1 t1=new TestMultiNaming1();
TestMultiNaming1 t2=new TestMultiNaming1();
System.out.println("Name of ... |
Can you give me an Example of naming a thread : Without Using setName() Method? | One can also set the name of a thread at the time of the creation of a thread, without using the setName() method. Observe the following code.
FileName: ThreadNamingExample.java
// A Java program that shows how one can
// set the name of a thread at the time
// of creation of the thread
// import st... |
What is Current Thread? | The currentThread() method returns a reference of the currently executing thread.
public static Thread currentThread() |
Can you give me an Example of currentThread() method? | FileName: TestMultiNaming2.java
class TestMultiNaming2 extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[]){
TestMultiNaming2 t1=new TestMultiNaming2();
TestMultiNaming2 t2=new TestMultiNaming2();
t1.star... |
What is the Priority of a Thread (Thread Priority)? | Each thread has a priority. Priorities are represented by a number between 1 and 10. In most cases, the thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses. Note that not on... |
What is Setter & Getter Method of Thread Priority? | Let's discuss the setter and getter method of the thread priority.
public final int getPriority(): The java.lang.Thread.getPriority() method returns the priority of the given thread.
public final void setPriority(int newPriority): The java.lang.Thread.setPriority() method updates or assign the priority of the thr... |
What are the 3 constants defined in Thread class? | 1.public static int MIN_PRIORITY
2.public static int NORM_PRIORITY
3.public static int MAX_PRIORITY |
Can you give me an Example of priority of a Thread? | FileName: ThreadPriorityExample.java
// Importing the required classes
import java.lang.*;
public class ThreadPriorityExample extends Thread
{
// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement... |
Can you give me an Example of IllegalArgumentException? | We know that if the value of the parameter newPriority of the method getPriority() goes out of the range (1 to 10), then we get the IllegalArgumentException. Let's observe the same with the help of an example.
FileName: IllegalArgumentException.java
// importing the java.lang package
import java.lang.*;
... |
What is Daemon Thread in Java? | Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.
There are many java daemon threads running automatically e.g. gc, finalizer etc.
You can see all... |
What are the Points to remember for Daemon Thread in Java? | -It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.
-Its life depends on user threads.
-It is a low priority thread. |
Why JVM terminates the daemon thread if there is no user thread? | The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread. |
What are the Methods for Java Daemon thread by Thread class? | Methods for Java Daemon thread by Thread class
The java.lang.Thread class provides two methods for java daemon thread.
Method: public void setDaemon(boolean status)
Description: is used to mark the current thread as daemon thread or user thread.
Method: public boolean isDaemon()
Description: is used to check t... |
Can you give me a Simple example of Daemon thread in java? | public class TestDaemonThread1 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){//checking for daemon thread
System.out.println("daemon thread work");
}
else{
System.out.println("user thread work");
}
}
public static void main(String[] args){
TestDaemonThread... |
What is Java Thread Pool? | Java Thread pool represents a group of worker threads that are waiting for the job and reused many times.
In the case of a thread pool, a group of fixed-size threads is created. A thread from the thread pool is pulled out and assigned a job by the service provider. After completion of the job, the thread is containe... |
What are the Thread Pool Methods? | newFixedThreadPool(int s): The method creates a thread pool of the fixed size s.
newCachedThreadPool(): The method creates a new thread pool that creates the new threads when needed but will still use the previously created thread whenever they are available to use.
newSingleThreadExecutor(): The method creates a... |
What is the Advantage of Java Thread Pool? | Better performance It saves time because there is no need to create a new thread. |
Can you give me an Example of Java Thread Pool? | Let's see a simple example of the Java thread pool using ExecutorService and Executors.
File: WorkerThread.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s){
... |
What are the Risks involved in Thread Pools? | The following are the risk involved in the thread pools.
Deadlock: It is a known fact that deadlock can come in any program that involves multithreading, and a thread pool introduces another scenario of deadlock. Consider a scenario where all the threads that are executing are waiting for the results from the thread... |
How to Tune the Thread Pool? | The accurate size of a thread pool is decided by the number of available processors and the type of tasks the threads have to execute. If a system has the P processors that have only got the computation type processes, then the maximum size of the thread pool of P or P + 1 achieves the maximum efficiency. However, the ... |
What is ThreadGroup in Java? | Java provides a convenient way to group multiple threads in a single object. In such a way, we can suspend, resume or interrupt a group of threads by a single method call.
Note: Now suspend(), resume() and stop() methods are deprecated.
Java thread group is implemented by java.lang.ThreadGroup class.
A ThreadGro... |
What are the Constructors of ThreadGroup class? | Constructors of ThreadGroup class
There are only two constructors of ThreadGroup class.
Constructor: ThreadGroup(String name)
Description: creates a thread group with given name.
Constructor: ThreadGroup(ThreadGroup parent, String name)
Description: creates a thread group with a given parent group and name.
... |
What are the Methods of ThreadGroup class? | Methods of ThreadGroup class
There are many methods in ThreadGroup class. A list of ThreadGroup methods is given below.
Modifier and Type: void
Method: checkAccess()
Description: This method determines if the currently running thread has permission to modify the thread group.
Modifier and Type: int
Method: a... |
What is Java Shutdown Hook? | A special construct that facilitates the developers to add some code that has to be run when the Java Virtual Machine (JVM) is shutting down is known as the Java shutdown hook. The Java shutdown hook comes in very handy in the cases where one needs to perform some special cleanup work when the JVM is shutting down. Not... |
When does the JVM shut down? | The JVM shuts down when:
-user presses ctrl+c on the command prompt
-System.exit(int) method is invoked
-user logoff
-user shutdown etc. |
What is The addShutdownHook(Thread hook) method?
| The addShutdownHook() method of the Runtime class is used to register the thread with the Virtual Machine.
Syntax:
public void addShutdownHook(Thread hook){}
The object of the Runtime class can be obtained by calling the static factory method getRuntime(). For example:
Runtime r = Runtime.getRuntime(); |
Can you give me a Simple example of Shutdown Hook? | FileName: MyThread.java
class MyThread extends Thread{
public void run(){
System.out.println("shut down hook task completed..");
}
}
public class TestShutdown1{
public static void main(String[] args)throws Exception {
Runtime r=Runtime.getRuntime();
... |
What is Java Garbage Collection? | In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in java it is performed automatically. So, ja... |
What is the Advantage of Garbage Collection? | -It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
-It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts. |
How can an object be unreferenced? | There are many ways:
-By nulling the reference
-By assigning a reference to another
-By anonymous object etc. |
Can you give me a Simple Example of garbage collection in java? | public class TestGarbage1{
public void finalize(){System.out.println("object is garbage collected");}
public static void main(String args[]){
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
Output:
object is ga... |
What is Java Runtime class? | Java Runtime class is used to interact with java runtime environment. Java Runtime class provides methods to execute a process, invoke GC, get total and free memory etc. There is only one instance of java.lang.Runtime class is available for one java application. |
What are the Important methods of Java Runtime class? | Important methods of Java Runtime class
Method: public static Runtime getRuntime()
Description: returns the instance of Runtime class.
Method: public void exit(int status)
Description: terminates the current virtual machine.
Method: public void addShutdownHook(Thread hook)
Description: registers new hook th... |
What is Java Runtime exec() method? | public class Runtime1{
public static void main(String args[])throws Exception{
Runtime.getRuntime().exec("notepad");//will open a new notepad
}
} |
How to shutdown system in Java? | You can use shutdown -s command to shutdown system. For windows OS, you need to provide full path of shutdown command e.g. c:\\Windows\\System32\\shutdown.
Here you can use -s switch to shutdown system, -r switch to restart system and -t switch to specify time delay.
public class Runtime2{
public static void ... |
How to shutdown windows system in Java? | public class Runtime2{
public static void main(String args[])throws Exception{
Runtime.getRuntime().exec("c:\\Windows\\System32\\shutdown -s -t 0");
}
} |
How to restart system in Java? | public class Runtime3{
public static void main(String args[])throws Exception{
Runtime.getRuntime().exec("shutdown -r -t 0");
}
} |
What is Java Runtime freeMemory() and totalMemory() method? | In the given program, after creating 10000 instance, free memory will be less than the previous free memory. But after gc() call, you will get more free memory.
public class MemoryTest{
public static void main(String args[])throws Exception{
Runtime r=Runtime.getRuntime();
System.out.println("Total Mem... |
What is Synchronization in Java? | Synchronization in Java is the capability to control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread to access the shared resource. |
Why use Synchronization? | The synchronization is mainly used to
1.To prevent thread interference.
2.To prevent consistency problem. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.