How multi-threading works in Java?

Now multi-threading Java supports multi-threading that basically allows multiple tasks to execute concurrently without occupying much memory you might have seen when you are here when we are using an editor there is the thing going on internally what thing, for example, I am typing some essay let’s suppose I am time exam essay now in internally or the grammatical X and the Spellings are checked right this is called multi-threading this is a naive example of multi-threading so basically I am doing one task so concurrently several other tasks are happening on that particular task right.


I am writing concurrently there is an I mean to say a spelling check going on the grammatical mistakes if there are any chemical mistakes that have been also checking right so because all things are multi-threading now let’s move ahead now let’s talk about JDK JRE and JVM until this point I have used these words many times like JDK JRE and JVM now first let me show you this image is very important the bigger box that is JDK is the Java development toolkit right and the inner box that is JRE is the Java runtime environment right and inside JRE we have JVM some libraries and development tools okay.

Let’s go back to JVM java virtual machine there is no such thing as JVM right in as me what do you mean by this thing it doesn’t exist in reality okay it’s a virtual machine that holds all Java executions, okay all the executions that happen in Java are done with the help of this wherein it also managed memory right the heap memory the stack memory garbage collector right would you be my garbage collector.


Basically, whenever we use variables and we assign memories right we don’t deal with such things as filling the memory or allocating which we used to do in another programming language like Andrea locations memory mm then we have to free write malloc calloc and then free the memory these things are not done in Java we don’t have to deal with this compiler will do this do these things for us okay rather JVM will do this thing for us right so that’s all about JVM now JRE JRE is the Java runtime and why right it is responsible for running pre-compiled Java programs that mean they have been compiled.

Now for running them, JRE provides the environment okay it contains all the files and libraries to facilitate right running if Java code running the Java code right so this is the thing that all the libraries attached to a program or files GRE is responsible for running them okay it can’t be helped we can’t do any development in this but we can we do the development right so JDK is responsible for doing out if we want to do development JDK is the go-to guy right so in JDK we can do us we can run our development programs right the Java applications can be easily done with the help of JDK so JDK is nothing.


The JRE plus development tools what development tools command like Java C Java we’re in Java is used you know for running the program and Java C is used for compiling the program right we all know that we have seen the demo right so these are all the things that we need to know right about JRE and JT he and JVM so these things are important before we start I just have briefed you about these things okay let’s move ahead let’s figure you have to keep this in mind right now the variables the most important thing in Java is way to store your values right if you if we talk about in day-to-day life right.

If you want to store some water right what do you use we use the bottle if you want to store some maybe cereals and all those things right rice maybe some cereals and all those things we use a container similarly if you want to store some value in Java variables right we have to use variable where you do is a name of a memory location where data is stored right what do you mean by this thing so basically if you have observed in your day-to-day life we need variables right if you are trying to store some water you need a porter if you are trying to store cereals you need a container for that similarly if you’re trying to store anything in Java you need variables so for that there’s this specific syntax given to it right datatype wear name and well what do you mean by datatype okay.

Basically, the datatype is nothing but a way to tell your system that we are storing this type of data for example if you are trying to store integers you write Intel if you’re trying to store decimal numbers you write float right if you’re trying to store some boolean value you write bool so similarly, these data types tell the system that you are storing this type of values okay then the where name depicts its name for example if you’re trying to store the count of some numbers right you write the variable name as count.

It’s also a best practice to use the variable name as what follows in your program like in if you’re trying to count anything you write the variable name as count rather than X Y or Z and then the Val represents its value for example int X or int count is equal to 5 right so this is a way to write a variable now let’s see a demo so basically we are trying to create variables in two different techniques using two different techniques right so the first technique is that you write your variable declaration and initialization in the same line okay as I told you that everything in Java objects right now you see that I have written int roll number equal to 23 right is this the way yes.

It is how so basically what we are trying or what we have created here is an object in Java we have wrapper class so this int is converted to its wrapper class that is integer int ege r-right so that happens internally we don’t have to think about it or we don’t have to deal it right now as this course is only for beginners so right now you can just say that this is an object right with the help of wrapper class we can do that so variable declaration and initialization in the same line that is in roll number equal to 23 similarly for the name first name Adam right this is also one way of creating strings right.

We can create this string with the help of new keyword as well okay then variable declaration first that is int marks then later we do the initialization that is marks equal to 89 then we try to print all these values okay now quickly go ahead and now let’s do a demo on variables okay so the first technique was we created a variable count equal to 10 so this is what type of declaration that is declaration and initialization in the same line then if we do another thing and like in ain’t do equal to in do okay then do equal to 2 so declaring it first and later initializing it okay then if you try to print this thing how we print our things system dot out dot println we know what this statement does right so then we did what we do count divided by do so what should be the output 10 divided by 2 that is 5 so let’s go and run this thing give it some time you see how output is 5 so this is how we declare our variables by is this statement the first statement is declaration and initializing initialization in the same line then this is Declaration and then later initialization.

Leave a Comment