当前位置: 首页 > news >正文

烟台市建委网站旧版优化大师

烟台市建委网站,旧版优化大师,网站首页布局设计用什么,永州网站建设收费情况目录 多线程编程 线程的概念与生命周期 创建线程的两种方式(继承Thread类、实现Runnable接口) 线程同步与锁机制(synchronized、Lock) 线程池(ExecutorService) 线程间通信(wait、notify、notifyAll) 实践建议:编写多线程程序,模拟生产者-消费者问题。 反射机…

目录

多线程编程

线程的概念与生命周期

创建线程的两种方式(继承Thread类、实现Runnable接口)

线程同步与锁机制(synchronized、Lock)

线程池(ExecutorService)

线程间通信(wait、notify、notifyAll)

实践建议:编写多线程程序,模拟生产者-消费者问题。

反射机制

Class类与反射

获取类的信息(字段、方法、构造器)

动态创建对象与调用方法

实践建议:通过反射实现一个简单的依赖注入。

注解(Annotation)

内置注解(@Override、@Deprecated等)

自定义注解

元注解(@Target、@Retention等)

实践建议:自定义注解并实现简单的注解处理器。

网络编程

TCP/IP协议与Socket编程

UDP协议与DatagramSocket编程

HTTP协议与URLConnection

实践建议:编写一个简单的聊天程序。

数据库编程

JDBC简介

连接数据库(DriverManager、Connection)

执行SQL语句(Statement、PreparedStatement)

事务管理

数据库连接池(DBCP、C3P0、HikariCP)


  1. 多线程编程

    1. 线程的概念与生命周期

      // 1. 线程的概念与生命周期// 线程是程序执行的最小单位,一个程序可以有多个线程。
      // 线程有以下几种状态:
      // - NEW: 新建状态,线程已经创建但尚未开始执行。
      // - RUNNABLE: 运行状态,线程正在运行或等待被调度执行。
      // - BLOCKED: 阻塞状态,线程因等待锁而被阻塞。
      // - WAITING: 等待状态,线程正在等待其他线程的通知。
      // - TIMED_WAITING: 超时等待状态,线程正在等待超时。
      // - TERMINATED: 终止状态,线程执行完成或者异常终止。// 示例:线程状态的转换
      public class ThreadLifeCycle {public static void main(String[] args) {Thread thread = new Thread(() -> {System.out.println("Thread is running...");});System.out.println("Thread state: " + thread.getState());  // NEW 状态thread.start();System.out.println("Thread state: " + thread.getState());  // RUNNABLE 状态}
      }
    2. 创建线程的两种方式(继承Thread类、实现Runnable接口)

      // 2. 创建线程的两种方式// (1)继承 Thread 类
      class MyThread extends Thread {@Overridepublic void run() {System.out.println("Thread is running using Thread class!");}
      }public class ThreadExample {public static void main(String[] args) {// 创建并启动线程MyThread myThread = new MyThread();myThread.start();  // 调用 start() 方法启动线程}
      }// (2)实现 Runnable 接口
      class MyRunnable implements Runnable {@Overridepublic void run() {System.out.println("Thread is running using Runnable interface!");}
      }public class RunnableExample {public static void main(String[] args) {// 创建线程Thread thread = new Thread(new MyRunnable());thread.start();  // 调用 start() 方法启动线程}
      }
    3. 线程同步与锁机制(synchronized、Lock)

      // 3. 线程同步与锁机制// 线程同步:避免多个线程同时访问共享资源导致数据不一致的问题。
      // (1)使用 synchronized 关键字进行同步
      class Counter {private int count = 0;// 使用 synchronized 确保线程安全public synchronized void increment() {count++;}public int getCount() {return count;}
      }public class SynchronizedExample {public static void main(String[] args) throws InterruptedException {Counter counter = new Counter();// 创建两个线程并启动Thread thread1 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("Final count: " + counter.getCount());  // 输出结果应该是 2000}
      }// (2)使用 Lock 进行同步
      import java.util.concurrent.locks.Lock;
      import java.util.concurrent.locks.ReentrantLock;class LockCounter {private int count = 0;private Lock lock = new ReentrantLock();public void increment() {lock.lock();  // 获取锁try {count++;} finally {lock.unlock();  // 释放锁}}public int getCount() {return count;}
      }public class LockExample {public static void main(String[] args) throws InterruptedException {LockCounter counter = new LockCounter();// 创建两个线程并启动Thread thread1 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("Final count using Lock: " + counter.getCount());  // 输出结果应该是 2000}
      }

    4. 线程池(ExecutorService)

      // 4. 线程池(ExecutorService)// 使用线程池来管理线程,避免频繁的创建与销毁线程。
      // ExecutorService 是一个接口,提供了用于创建线程池和提交任务的方法。import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;public class ThreadPoolExample {public static void main(String[] args) {// 创建一个固定大小的线程池ExecutorService executorService = Executors.newFixedThreadPool(2);// 提交任务executorService.submit(() -> System.out.println("Task 1 is executed"));executorService.submit(() -> System.out.println("Task 2 is executed"));// 关闭线程池executorService.shutdown();}
      }

    5. 线程间通信(wait、notify、notifyAll)

      // 5. 线程间通信(wait、notify、notifyAll)// 使用线程间通信来协调多个线程的执行顺序。
      // - wait():使当前线程进入等待状态,并释放锁。
      // - notify():唤醒等待队列中的一个线程。
      // - notifyAll():唤醒等待队列中的所有线程。class SharedResource {private int count = 0;// 使用 wait 和 notify 进行线程间通信public synchronized void produce() throws InterruptedException {while (count >= 1) {wait();  // 如果资源已满,当前线程等待}count++;System.out.println("Produced, count: " + count);notify();  // 通知消费者}public synchronized void consume() throws InterruptedException {while (count <= 0) {wait();  // 如果资源为空,当前线程等待}count--;System.out.println("Consumed, count: " + count);notify();  // 通知生产者}
      }public class ProducerConsumerExample {public static void main(String[] args) throws InterruptedException {SharedResource resource = new SharedResource();// 创建并启动生产者线程Thread producer = new Thread(() -> {try {for (int i = 0; i < 5; i++) {resource.produce();}} catch (InterruptedException e) {e.printStackTrace();}});// 创建并启动消费者线程Thread consumer = new Thread(() -> {try {for (int i = 0; i < 5; i++) {resource.consume();}} catch (InterruptedException e) {e.printStackTrace();}});producer.start();consumer.start();producer.join();consumer.join();}
      }
    6. 实践建议:编写多线程程序,模拟生产者-消费者问题。

  2. 反射机制

    1. Class类与反射

      
      反射机制是 Java 提供的一个强大特性,它允许在运行时动态地查询和操作类的结构(如字段、方法、构造器等),以及动态创建对象和调用方法。// 1. Class类与反射// 在 Java 中,`Class` 是一个类,用于描述一个类的信息,
      // 每个类在 JVM 中都有一个与之对应的 `Class` 对象,通过该对象可以获取类的各种信息。
      // 获取 `Class` 对象的方式有几种:// (1)使用 `getClass()` 方法
      Object obj = new String("Hello");
      Class<?> clazz1 = obj.getClass();  // 获取对象的 Class 对象
      System.out.println(clazz1.getName());  // 输出类的全限定名:java.lang.String// (2)使用 `Class.forName()` 方法
      Class<?> clazz2 = Class.forName("java.lang.String");  // 通过类名获取 Class 对象
      System.out.println(clazz2.getName());  // 输出类的全限定名:java.lang.String// (3)使用 `.class` 语法
      Class<?> clazz3 = String.class;  // 获取 String 类的 Class 对象
      System.out.println(clazz3.getName());  // 输出类的全限定名:java.lang.String

      <
http://www.shuangfujiaoyu.com/news/47580.html

相关文章:

  • 苹果手机怎么做ppt下载网站苏州新闻今天最新消息新闻事件
  • 个人服务器 网站建设鄂州网站seo
  • 台州网站建设网站推广网上推广的平台有哪些
  • 广州做淘宝的化妆品网站好百度灰色关键词排名
  • 北京pk10盘制作网站建设网站引流推广软件
  • 通付盾 网站建设数字营销案例
  • wordpress课堂主题优化营商环境工作开展情况汇报
  • 做网站怎么写预算重庆森林在线观看
  • 网站建设报告论文百度文库搜索关键词
  • 济南网站制作 泉诺友链交易平台
  • 做网站漯河最好用的手机优化软件
  • 怎么做返利网站磁力云搜索引擎入口
  • 花生壳怎么做网站seo做关键词怎么收费的
  • 网站建设seo视频浙江seo推广
  • 怎样修改静态公司网站页面电话廊坊seo排名公司
  • office做的网站怎么发布seo服务合同
  • 东莞网站推广公司宁波seo外包方案
  • 做网站需要哪些资料网站优化外包公司
  • 如何批量做网站口碑营销怎么做
  • 怎么使用织梦做网站常州seo
  • WordPress设置会话有效时间优化关键词的方法有哪些
  • 无锡网站seosem培训学校
  • 自建站搭建win7优化大师官网
  • 网站建设制作 企业站开发哪家好南昌seo技术外包
  • 做网站 搜索引擎博客seo优化技术
  • 自己做网站卖东西需要交税吗seo排名赚app最新版本
  • 盘锦威旺做网站建设公司公司网页设计
  • 建设工程类的网站杭州谷歌推广
  • 汕头澄海玩具合肥seo软件
  • 怎么检查网站死链给公司做网站要多少钱