随笔分类 - Java学习
摘要:接口定义 public interface Eatable { void eat(); } View Code 接口实现类 public class EatableImp implements Eatable{ @Override public void eat() { System.out.pri
阅读全文
摘要:public class LambdaDemo { public static void main(String[] args) { //正常实现一个多线程程序 MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Scoket对象 Socket s= new Socket("192.168.50.76",4444
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Socket对象 Socket s = new Socket("192.168.50.76", 33
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Socket对象 Socket s= new Socket("192.168.50.76",2222
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Socket对象 Socket s = new Socket("192.168.50.76",111
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Socket对象 Socket s= new Socket("192.168.50.76",1234
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端Sokcet Socket s = new Socket("192.168.50.76", 1999
阅读全文
摘要:客户端代码: public class ClientDemo { public static void main(String[] args) throws IOException { //创建客户端的Socket对象 Socket s = new Socket("192.168.50.76",12
阅读全文
摘要:发送端代码: /* UDP发送数据:数据来自于键盘录入,如果输入的数据是886,发送数据结束 */ public class SendDemo { public static void main(String[] args) throws IOException { //1.创建发送端对象 Data
阅读全文
摘要:public class ReceivedDemo { public static void main(String[] args) throws IOException { //1.创建接收端Socket对象 DatagramSocket ds = new DatagramSocket(10086
阅读全文
摘要:public class SendDemo { public static void main(String[] args) throws IOException { //1.创建发送端Socket对象 // DatagramSocket()构造数据报套接字并将其绑定到本地主机上的任何可用端口。 D
阅读全文
摘要:public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { //public static InetAddress getByName(String host)
阅读全文
摘要:案例:牛奶工(生产者)送牛奶,送第几瓶奶时,消费者就接受第几瓶,比如送第一瓶奶,消费者就收到第一瓶奶。 使用奶箱(Milk Box)来作为共享数据区域。 那么在生产者类中就要有存储牛奶的方法,消费者就要有获取牛奶的方法。 奶箱类(Milk Box),需要对方法添加synchronized关键则,否则
阅读全文
摘要:使用try...finally...来保证释放锁unlock()一定会被执行。 public class SellTickets implements Runnable{ //共有100张票 private int tickets = 100; private Object obj = new Ob
阅读全文
摘要:public class SellTickets implements Runnable{ //共有100张票 private int tickets = 100; private Object obj = new Object(); private int x = 0; @Override pub
阅读全文
摘要:在卖票案例中会出现两个问题: 1.多个线程执行同一条语句产生了冲突 2.导致数据出现异常 以上两个问题可以总结为:数据安全问题。 Java提供了解决数据安全的方式:使用同步代码块 public class SellTickets implements Runnable{ //共有100张票 priv
阅读全文
摘要:public class SellTickets implements Runnable{ //共有100张票 private int tickets = 100; @Override public void run() { //票数大于0就买票,并告知是哪个窗口卖的 //每卖一张后,总票数要减1
阅读全文
摘要:public class MyRunnable implements Runnable { @Override public void run() { for (int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName
阅读全文
摘要:static void sleep(long millis) 使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。 public class ThreadSleep extends Thread{ @Override public void run(
阅读全文

浙公网安备 33010602011771号