Forwarded from عبدالرحیم شریفی
Sleeping a thread
متود sleep از کلاس Thread، برای متوقف کردن ترد برای یه مدت زمان خاص استفاده میشه.
Example of sleep method in java
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(500);
}catch(InterruptedException 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
4همین طور که میدونید در یک لحظه فقط یک ترد اجرا میشه. بخاطر همین وقتی در این مثال☝️یک ترد برای یه مدت زمانی به حالت sleep میره، thread shedular یه thread دیگه رو اجرا میکنه و الاآخر.
Forwarded from عبدالرحیم شریفی
Start a thread twice
بعد از start کردن یک thread، اون دیگه نمیتونه دوباره start بشه. اگر همچین کاری انجام بدید یه IllegalThreadStateException پرتاب (throw) میشه.
به مثال زیر 👇توجه کنید.
public class TestThreadTwice1 extends Thread{
public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestThreadTwice1 t1=new TestThreadTwice1();
t1.start();
t1.start();
}
}
--------------------------------------------
Output:
running
Exception in thread "main" java.lang.IllegalThreadStateExceptionForwarded from عبدالرحیم شریفی
Java Socket Programming
جاوا سوکت programming برای ارتباط بین برنامه هایی که در JRE های متفاوت اجرا میشن، بکار برده میشه.
هم میتونه connection-oriented هم میتونه connection-less باشه.
از کلاس های Socket و ServerSocket برای connection-oriented و از کلاس های DatagramSocket و DatagramPacket برای connection-less استفاده میشه.
کلاینت در سوکت programming باید دوتا چیز رو بدونه:
۱- آی پی سرور
۲- شماره پورت (port number)
Socket class
این کلاس برای ساخت socket client استفاده میشه، و ۳ متود مهم داره:
1- public InputStream getInputStream()
2- public OutputStream getOutputStream()
3- public synchronized void close()
۱- از سوکت، InputStream رو برای ورود اطلاعات میگیره.
۲- از سوکت، OutputStream رو برای خروجی اطلاعات میگیره.
۳- سوکت رو می بنده.
ServerSocket class
این کلاس برای ساخت socket server استفاده میشه که با کلاینت ها در ارتباط هست، و دوتا متود مهم داره:
1- public Socket accept()
2- public synchronized void close()
۱- یه سوکت برمی گردونه و یه ارتباط بین سرور و کلاینت برقرار میکنه.
۲- سوکت سرور رو می بنده.
Example of Java Socket Programming
در این مثال👇با استفاده از socket programming یه پیام از کلاینت ارسال میشه و سرور اونو دریافت میکنه.
File: MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){
System.out.println(e);
}
}
}
File: MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){
System.out.println(e);
}
}
}
برای اجرا کردن این مثال☝️هر فایل (برنامه) رو به صورت جداگانه اجرا کنید.
Example of Java Socket Programming (Read-Write both side)
در این مثال👇ابتدا کلاینت، یه متنی رو برای سرور می فرسته. وقتی سرور متن رو دریافت میکنه، اونو پرینت میکنه. سپس سرور یه متنی رو برای کلاینت می فرسته و کلاینت پس از دریافت، اونو پرینت میکنه.
File: MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
File: MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}
Java Memory Management
English | MP4 | AVC 1280×720 | AAC 48KHz 2ch | 3 Hours | 370 MB
What Java professionals need to know about memory, garbage collection, tuning the VM, and avoiding memory leaks.
This is a course for Java professionals (or aspiring Java professionals) who need an in depth understanding of how memory works in Java. In this course you’ll learn what kinds of memory leaks are possible in Java, and how to avoid them. In addition I’ll show you tools you can use to analyse how your applications are performing, and detect inefficient memory use, such as objects which are taking up too much memory, or problems causing inefficient garbage collection. And we’ll see how to fix these.
You don’t need to be a Java Expert to do this course, but you should be able to confidently create basic Java code before you start.
https://coderprog.com/java-memory-management/
@java_me
English | MP4 | AVC 1280×720 | AAC 48KHz 2ch | 3 Hours | 370 MB
What Java professionals need to know about memory, garbage collection, tuning the VM, and avoiding memory leaks.
This is a course for Java professionals (or aspiring Java professionals) who need an in depth understanding of how memory works in Java. In this course you’ll learn what kinds of memory leaks are possible in Java, and how to avoid them. In addition I’ll show you tools you can use to analyse how your applications are performing, and detect inefficient memory use, such as objects which are taking up too much memory, or problems causing inefficient garbage collection. And we’ll see how to fix these.
You don’t need to be a Java Expert to do this course, but you should be able to confidently create basic Java code before you start.
https://coderprog.com/java-memory-management/
@java_me
Vaiualise, document and explore your software architecture with the C4 model
https://structurizr.com/
https://structurizr.com/