新手初学Java网络编程

网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来。本文介绍了一些网络编程基础的概念,并用Java来实现TCP和UDP的Socket的编程,来让读者更好的了解其原理

运行线程

创建Thread的子类

 public class ThreadChild extends Thread { @Override public void run() { while (true) { System.out.println("run"); } } }
 public class Test { public static void main(String[] args) throws Exception { ThreadChild t = new ThreadChild(); t.start(); } }

创建Thread传入Runnable接口实现类

 public class RunnableImpl implements Runnable { @Override public void run() { while (true) { System.out.println("run"); } } }
 public class Test { public static void main(String[] args) throws Exception { Thread t = new Thread(new RunnableImpl()); t.start(); } }

回调

ExecutorService线程池的Submit()可接收Runnable或者Callable.Callable执行结束后有返回值,Runnable执行结束后没有返回值.可以通过submit的返回对象Future的get方法获取返回返回值,需要注意的是get方法是一个阻塞方法.若线程没有执行完毕,则会阻塞get()直到线程执行结束后返回数据.

 public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(10); Future future = executorService.submit(() -> { TimeUnit.SECONDS.sleep( 3); return 1; }); Future future2 = executorService.submit(() -> { }); Future future3 = executorService.submit(() -> { }, "aaa"); System.out.println(future.get()); System.out.println(future2.get()); System.out.println(future3.get()); }

同步方法

synchronized关键字和方法组合使用后,该方法是一个同步方法。同步方法都有一个隐试的锁,多个线程同时执行该方法时,只能顺序执行,未强到锁的线程处于阻塞状态。另外静态方法和非静态方法使用的是不同的锁,非静态方法的锁是对象锁,若两个线程通过两个该类的对象调用那么互不影响。静态方法是类锁,即本类的Class对象。同步方法无法指定锁资源,要么是this锁,要么是Class锁。

 public class Test { public static void main(String[] args) throws Exception { Test test = new Test(); Thread t1 = new Thread(()->{ test.synFunction(); }); Thread t2 = new Thread(()->{ test.synFunction(); }); t1.start(); t2.start(); } public synchronized void synFunction(){ try { System.out.println("-------------"); TimeUnit.SECONDS.sleep(3); }catch (Exception e) {} } }
 public class Test { public static void main(String[] args) throws Exception { Thread t1 = new Thread(()->{ Test.synFunction(); }); Thread t2 = new Thread(()->{ Test.synFunction(); }); t1.start(); t2.start(); } public static synchronized void synFunction(){ try { System.out.println("-------------"); TimeUnit.SECONDS.sleep(3); }catch (Exception e) {} } }

同步块

同步代码块可以指定锁对象,非静态方法可以指定任意的对象锁,或者任意的类锁。但静态方法只能使用任意的类锁。

 public class Test { public static void main(String[] args) throws Exception { Test lock = new Test(); Thread t1 = new Thread(() -> { lock.synFunction(); }); Thread t2 = new Thread(() -> { lock.synFunction(); }); t1.start(); t2.start(); } public void synFunction() { synchronized (this) {//对象锁 try { System.out.println("-------------"); TimeUnit.SECONDS.sleep(3); } catch (Exception e) { } } } }
 public class Test { public static void main(String[] args) throws Exception { Test lock1 = new Test(); Test lock2 = new Test(); Thread t1 = new Thread(() -> { lock1.synFunction(lock1); }); Thread t2 = new Thread(() -> { lock2.synFunction(lock2); }); t1.start(); t2.start(); } public void synFunction(Object lock) { synchronized (lock) {//对象锁 try { System.out.println("-------------"); TimeUnit.SECONDS.sleep(3); } catch (Exception e) { } } } }
 public class Test { public static void main(String[] args) throws Exception { Test lock1 = new Test(); Test lock2 = new Test(); Thread t1 = new Thread(() -> { lock1.synFunction(); }); Thread t2 = new Thread(() -> { lock2.synFunction(); }); t1.start(); t2.start(); } public void synFunction()

以上就是新手初学Java网络编程的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java