【高并发系列】3、线程那些事儿

LMAX高并发系统架构 很早就看到过MF的这篇The LMAX Architecture,可是之前一来英文水平不够,二来确实看不懂… 今天有幸再次看到,一口气读完终于有所领悟。1 Overall ArchitectureLMAX是一个新的金融交易平台。系统完全构建于JVM之上,却能在一个单线程上每秒处理6m的订单(其实是指核心业务逻辑处理类是单线程的)。系统主要由高并发组件Disruptors和业务服务Business 阅读详情

线程是轻量级进程,是程序执行的最小单位;

线程的生命周期:

线程基本操作:

1、新建线程

Thread t = new Thread();
t.start();

Thread的run()方法只会在当前线程中串行执行run()方法中的代码,正确开启新线程应使用start()方法;

public class CreateThread implements Runnable {

	@Override
	public void run() {
		System.out.println("CreateThread.run()");
	}

	public static void main(String[] args) {
		Thread t = new Thread(new CreateThread());
		t.start();
	}

}

默认的Thread.run()方法就是直接调用内部Runnable接口;

/* What will be run. */
private Runnable target;

/**
 * If this thread was constructed using a separate
 * <code>Runnable</code> run object, then that
 * <code>Runnable</code> object's <code>run</code> method is called;
 * otherwise, this method does nothing and returns.
 * <p>
 * Subclasses of <code>Thread</code> should override this method.
 *
 * @see     #start()
 * @see     #stop()
 * @see     #Thread(ThreadGroup, Runnable, String)
 */
@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

2、终止线程

stop()方法之所以被废弃,是因为stop()方法过于暴力,强行把正在执行的线程终止,可能会引起一些数据不一致的问题;

不安全的停止线程demo:

public class StopThreadUnsafe {
	private static User u = new User();
	public static class User {
		private int id;
		private String name;
		public User() {
			id = 0;
			name = "0";
		}
		public void setId(int id) {
			this.id = id;
		}
		public int getId() {
			return id;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getName() {
			return name;
		}
		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + "]";
		}
	}
	public static class ChangeObjectThread extends Thread {
		@Override
		public void run() {
			while (true) {
				synchronized (u) {
					int v = (int) (System.currentTimeMillis() / 1000);
					u.setId(v);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					u.setName(String.valueOf(v));
				}
				Thread.yield();
			}
		}
	}
	public static class ReadObjectThread extends Thread {
		@Override
		public void run() {
			while (true) {
				synchronized (u) {
					if (u.getId() != Integer.parseInt(u.getName())) {
						System.out.println(u);
					}
				}
				Thread.yield();
			}
		}
	}
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws InterruptedException {
		new ReadObjectThread().start();
		while (true) {
			Thread t = new ChangeObjectThread();
			t.start();
			Thread.sleep(150);
			t.stop();
		}
	}
}

##################################################
console
User [id=1549372735, name=1549372734]
User [id=1549372735, name=1549372734]
User [id=1549372735, name=1549372734]
User [id=1549372736, name=1549372735]

正确停止线程demo:

public class StopThreadSafe {
	private static User u = new User();
	public static class User {
		private int id;
		private String name;
		public User() {
			id = 0;
			name = "0";
		}
		public void setId(int id) {
			this.id = id;
		}
		public int getId() {
			return id;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getName() {
			return name;
		}
		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + "]";
		}
	}
	public static class ChangeObjectThread extends Thread {
		private volatile boolean stopme = false;
		public void stopMe() {
			stopme = true;
		}
		@Override
		public void run() {
			while (true) {
				if (stopme) {
					System.out.println("exit by stop me");
					break;
				}
				synchronized (u) {
					int v = (int) (System.currentTimeMillis() / 1000);
					u.setId(v);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					u.setName(String.valueOf(v));
				}
				Thread.yield();
			}
		}
	}
	public static class ReadObjectThread extends Thread {
		@Override
		public void run() {
			while (true) {
				synchronized (u) {
					if (u.getId() != Integer.parseInt(u.getName())) {
						System.out.println(u);
					}
				}
				Thread.yield();
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		new ReadObjectThread().start();
		while (true) {
			ChangeObjectThread t = new ChangeObjectThread();
			t.start();
			Thread.sleep(150);
			t.stopMe();
		}
	}
}

#####################
console
exit by stop me
exit by stop me
exit by stop me

3、线程中断

线程中断是一种重要的线程协作机制;

public void interrupt()                // 中断线程
public boolean isInterrupted()         // 判断是否被中断
public static boolean interrupted()    // 判断是否被中断,并清除当前中断状态
public void interrupt() {
    if (this != Thread.currentThread())
        checkAccess();
    synchronized (blockerLock) {
        Interruptible b = blocker;
        if (b != null) {
            interrupt0();           // Just to set the interrupt flag
            b.interrupt(this);
            return;
        }
    }
    interrupt0();
}
public boolean isInterrupted() {
    return isInterrupted(false);
}
public static boolean interrupted() {
    return currentThread().isInterrupted(true);
}
/**
 * Tests if some Thread has been interrupted.  The interrupted state
 * is reset or not based on the value of ClearInterrupted that is
 * passed.
 */
private native boolean isInterrupted(boolean ClearInterrupted);

 使用Thread.currentThread().isInterrupted()判断当前线程是否被中断,如果被中断了,退出循环体,结束线程;

public class InterruptThread {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread("interrupt") {
			@Override
			public void run() {
				while (true) {
					if (Thread.currentThread().isInterrupted()) {
						System.out.println("Interrupted!");
						break;
					}
				}
				Thread.yield();
			}
		};
		t.start();
		TimeUnit.SECONDS.sleep(2);
		t.interrupt();
	}
}

Thread.sleep()函数签名:

/**
 * Causes the currently executing thread to sleep (temporarily cease
 * execution) for the specified number of milliseconds, subject to
 * the precision and accuracy of system timers and schedulers. The thread
 * does not lose ownership of any monitors.
 *
 * @param  millis
 *         the length of time to sleep in milliseconds
 *
 * @throws  IllegalArgumentException
 *          if the value of {@code millis} is negative
 *
 * @throws  InterruptedException
 *          if any thread has interrupted the current thread. The
 *          <i>interrupted status</i> of the current thread is
 *          cleared when this exception is thrown.
 */
public static native void sleep(long millis) throws InterruptedException;

当线程在休眠被中断时会抛出InterruptedException异常,并清除中断标记,catch捕获后没有选择立即退出线程,而是设置中断标志,保证数据的一致性和完整性; 

public class InterruptSleepThread {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread("interrupt-sleep") {
			@Override
			public void run() {
				while (true) {
					if (Thread.currentThread().isInterrupted()) {
						System.out.println("Interrupted!");
						break;
					}
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						System.out.println("Interrupted when sleep.");
						// 设置中断状态
						Thread.currentThread().interrupt();
					}
				}
				Thread.yield();
			}
		};
		t.start();
		Thread.sleep(1500);
		t.interrupt();
	}
}

4、等待wait()和通知notify()

public final native void wait(long timeout) throws InterruptedException
public final native void notify()
public final native void notifyAll()

Object.wait()方法和Thread.sleep()方法都让线程等待若干时间,wait()方法可以被唤醒,并且wait()方法会释放目标对象的锁,但sleep()方法不会释放任何资源;

public class WaitNotifyDemo {
	private static final Object lock = new Object();
	public static class WaitThread extends Thread {
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println(System.currentTimeMillis() + ":WaitThread start!");
				try {
					System.out.println(System.currentTimeMillis() + ":WaitThread wait for lock.");
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(System.currentTimeMillis() + ":WaitThread end!");
			}
		}
	}
	public static class NotifyThread extends Thread {
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println(System.currentTimeMillis() + ":NotifyThread start!");
				lock.notify();
				System.out.println(System.currentTimeMillis() + ":NotifyThread end!");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		Thread waitThread = new WaitThread();
		Thread notifyThread = new NotifyThread();
		waitThread.start();
		notifyThread.start();
	}
}

1549382736972:WaitThread start!
1549382736972:WaitThread wait for lock.
1549382736972:NotifyThread start!
1549382736972:NotifyThread end!
1549382738977:WaitThread end!

5、挂起suspend()和继续执行resume()线程

/**
 * Suspends this thread.
 * <p>
 * First, the <code>checkAccess</code> method of this thread is called
 * with no arguments. This may result in throwing a
 * <code>SecurityException </code>(in the current thread).
 * <p>
 * If the thread is alive, it is suspended and makes no further
 * progress unless and until it is resumed.
 *
 * @exception  SecurityException  if the current thread cannot modify
 *               this thread.
 * @see #checkAccess
 * @deprecated   This method has been deprecated, as it is
 *   inherently deadlock-prone.  If the target thread holds a lock on the
 *   monitor protecting a critical system resource when it is suspended, no
 *   thread can access this resource until the target thread is resumed. If
 *   the thread that would resume the target thread attempts to lock this
 *   monitor prior to calling <code>resume</code>, deadlock results.  Such
 *   deadlocks typically manifest themselves as "frozen" processes.
 *   For more information, see
 *   <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 */
@Deprecated
public final void suspend() {
    checkAccess();
    suspend0();
}

/**
 * Resumes a suspended thread.
 * <p>
 * First, the <code>checkAccess</code> method of this thread is called
 * with no arguments. This may result in throwing a
 * <code>SecurityException</code> (in the current thread).
 * <p>
 * If the thread is alive but suspended, it is resumed and is
 * permitted to make progress in its execution.
 *
 * @exception  SecurityException  if the current thread cannot modify this
 *               thread.
 * @see        #checkAccess
 * @see        #suspend()
 * @deprecated This method exists solely for use with {@link #suspend},
 *     which has been deprecated because it is deadlock-prone.
 *     For more information, see
 *     <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 *     are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 */
@Deprecated
public final void resume() {
    checkAccess();
    resume0();
}

suspend()方法挂起线程时导致线程暂停,但不会释放任何锁资源;如果resume()方法意外地在suspend()方法前就执行了,会导致被挂起的线程可能很难有机会被继续执行;并且对于被挂起的线程,从线程状态上看居然还是Runnable;

public class SuspendResumeDemo {
	public static Object lock = new Object();
	private static ChangeObjectThread t1 = new ChangeObjectThread("t1");
	private static ChangeObjectThread t2 = new ChangeObjectThread("t2");
	public static class ChangeObjectThread extends Thread {
		public ChangeObjectThread(String name) {
			super.setName(name);
		}

		@SuppressWarnings("deprecation")
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("In " + getName());
				Thread.currentThread().suspend();
			}
		}
	}
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws InterruptedException {
		t1.start();
		Thread.sleep(100);
		t2.start();
		t1.resume();
		t2.resume();
		t1.join();
		t2.join();
	}
}

In t1
In t2

(t1启动后,持有lock锁,而且suspend()方法不会释放资源,一直持有锁,当t2启动后,无法获得lock锁,t2阻塞;当t1.resume()执行后,t1继续执行,释放lock锁,此时t2获得lock锁,打印In t2,之后t2挂起;与此同时main线程中执行了t2.resume(),这个resume方法没有生效,造成t2永远挂起,并且永远占有lock锁;)

示例代码中,t2线程的resume()方法就在其suspend()方法前执行了,导致t2线程被挂起,程序不会退出,永远被挂起,并且永远占用lock对象的锁;

使用wait()方法和notify()方法模拟挂起和继续执行功能:

public class WaitNotifySimulateSuspendResumeDemo {
	public static Object lock = new Object();
	public static class ChangeObjectThread extends Thread {
		private volatile boolean suspendme = false;
		public void suspendMe() {
			suspendme = true;
		}
		public void resumeMe() {
			suspendme = false;
			synchronized (this) {
				notify();
			}
		}
		@Override
		public void run() {
			while (true) {
				synchronized (this) {
					while (suspendme) {
						try {
							wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				synchronized (lock) {
					System.out.println("In ChangeObjectThread");
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Thread.yield();
			}
		}
	}
	public static class ReadObjectThread extends Thread {
		@Override
		public void run() {
			while (true) {
				synchronized (lock) {
					System.out.println("In ReadObjectThread");
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Thread.yield();
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		ChangeObjectThread t1 = new ChangeObjectThread();
		ReadObjectThread t2 = new ReadObjectThread();
		t1.start();
		t2.start();
		Thread.sleep(1000);
		t1.suspendMe();
		System.out.println("suspend t1 2 seconds.");
		Thread.sleep(2000);
		System.out.println("resume t1");
		t1.resumeMe();
		t1.join();
		t2.join();
	}
}

6、等待线程结束join()和谦让yeild()

public final void join() throws InterruptedException
public final synchronized void join(long millis) throws InterruptedException
public class JoinDemo {
	public volatile static int i = 0;
	public static class IncrementThread extends Thread {
		@Override
		public void run() {
			for (i = 0; i < 10000000; i++)
				;
		}
	}
	public static void main(String[] args) throws InterruptedException {
		IncrementThread it = new IncrementThread();
		it.start();
		it.join();
		System.out.println(i);
	}
}

输出i的值为10000000,即it.join()保证了main线程等待IncrementThread执行完毕了之后,再输出i的值;

public static native void yield();

yield()方法会让出CPU,还会进行CPU资源的争夺,但是是否能够再次被分配时间片就不一定了;

Win7 x64位系统下几乎完美使用安装RTX3050 6G显卡驱动详细教程 RTX3050 6G显卡驱动安装指南:由于该显卡发布时间晚于驱动,需手动修改安装文件。首先在NVIDIA官网下载驱动并解压,通过设备管理器获取硬件ID(PCI\VEN_10DE&DEV_2584),然后在解压目录的.inf文件中添加对应的设备标识。安装完成后可能出现设备管理器感叹号问题,推荐通过命令提示符启用测试模式(bcdedit /set testsigning on)解决,此方法无需每次启动按F8,但会显示测试模式水印。文中提供了详细的图文操作步骤和两种解决方案。 阅读详情

相关推荐

别再手动算临界转速了!用ANSYS Workbench 2023 R2快速搞定转子坎贝尔图(附完整APDL命令流)

本文详细介绍了如何利用ANSYS Workbench 2023 R2快速生成转子坎贝尔图,实现全自动临界转速分析。通过对比传统手动计算与Workbench自动化流程的效率差异,展示了其在旋转机械设计中的革命性优势,包括大幅缩短分析周期、提升精度至98.7%。文章还提供了完整的APDL命令流和实战案例,帮助工程师快速掌握这一高效工具。

weixin_42699679的博客 472

创建并启动线程几种方式

一:创建线程的几种方式 1.第一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法,然后在run方法里填写相应的逻辑代码。class ThreadDemo1 extends Thread{ @Override public void run() { for(int i =0;i<5;i++){ Sys

追梦的疯子 5262

R语言,charls数据库清洗、基线特征描述、logistics回归、rcs模型、森林图

【代码】R语言,charls数据库清洗、基线特征描述、logistics回归、rcs模型、森林图。

2402_87751128的博客 2512

多线程 第二节 线程创建与线程安全

1、 线程间的互斥 在Java中,用互斥锁来保证共享数据操作的完整性。 可以在任意对象及方法上加锁,而加锁的这段代码称为互斥区或临界区。 2、 Synchronized的使用方式 3、 Synchronized 锁重入 最大的作用是避免死锁 可重入锁具有父子可继承性 4、 Java 虚拟机对 synchronized 的优化 在 Java SE 1.6 中,锁一共有4种状态,级别从低到高依次是:无锁状态、偏向锁状态、轻量级锁状态、重量级锁状态 锁可以升级但...

ellen艾琳的博客 238

Proteus 8.9仿真五路抢答器:从74LS148编码到NE555报警的保姆级电路拆解

本文详细解析了使用Proteus 8.9仿真五路抢答器的完整设计流程,从74LS148优先编码器到NE555定时报警电路。通过保姆级的电路拆解和芯片选型指南,帮助读者掌握数字电路中的编码、锁存、译码和定时等核心技术,实现稳定可靠的抢答系统仿真。

weixin_30836759的博客 380

EPS+ArcGIS+CASS三剑客:等高线裁剪的完整工作流与避坑指南

本文详细介绍了EPS、ArcGIS和CASS三款软件在等高线裁剪中的协同工作流程与避坑指南。通过发挥各软件优势,解决数据完整性、拓扑准确性和流程可靠性等核心挑战,提升40%以上的工作效率。特别适合大型测绘项目中复杂地形的处理,涵盖数据准备、核心处理到后期优化的全流程。

weixin_30352645的博客 618

Android创建线程的方式及启动方式分析

本文介绍两方面的知识: 一.创建线程的两种方式 二.启动线程start和run的分析   一.创建线程的两种方式     java提供两种创建线程的方式: 1.继承Thread private void createThread(){     new MyThread().start(); } private class MyThread extends Thread{    ...

认真做事,开心生活 4449

Goose开源程序本地机上 AI 代理,能够从头到尾自动执行复杂的开发任务。Goose 不仅可以提供代码建议,还可以自主构建整个项目、编写和执行代码、调试故障、编排工作流程以及与外部 API 交互

Goose开源程序 是您的本机上 AI 代理,能够从头到尾自动执行复杂的开发任务。Goose 不仅可以提供代码建议,还可以自主构建整个项目、编写和执行代码、调试故障、编排工作流程以及与外部 API 交互。无论您是在构建想法原型、优化现有代码,还是管理复杂的工程管道,goose 都能适应您的工作流程并精确执行任务。

struggle2025的博客 2041

windows编程学习——CreateThread()函数

CreateThread()函数介绍: HANDLE CreateThread(   LPSECURITY_ATTRIBUTES     lpThreadAttributes,        //指向SECURITY_ATTRIBUTES的指针,NULL默认为安全,不可被子线程继承   DWORD                                    dwStackSize,  ...

Cry_Alone的博客 2187

Windows显卡TDR超时导致Unity崩溃?手把手教你修改注册表延长响应时间

本文详细解析了Windows显卡TDR超时导致Unity崩溃的问题,并提供了修改注册表延长响应时间的完整解决方案。通过调整TdrDelay和TdrDdiDelay键值,开发者可以有效避免D3D11设备丢失错误,提升Unity编辑器的稳定性。文章还包含显卡驱动优化、系统性能调优等实用技巧,帮助开发者彻底解决编辑器崩溃问题。

weixin_33737774的博客 623

华为OD机试双机位C卷-用户入网定期复评(C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【用户入网定期复评】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【用户入网定期复评】。

qq_45776114的博客 837

Java并发编程:1.线程的创建和运行

文章目录1. CPU、进程、线程的关系2. 线程的创建与启动2.1 线程的创建2.2 启动线程start()和run()的区别。 1. CPU、进程、线程的关系 在很久很久以前,计算机还没有操作系统,它们只能运行一个程序,这个程序直接访问机器的所有资源。 操作系统出现后,一台计算机可以运行多个程序了,它们在各自的进程(processes)中运行。每一个进程都是一个独立的运行单位,也是系统进行资源分...

克己的博客 315

第1讲|AI产品经理全景图谱:不是“会用AI工具”,而是能对AI产品结果负责

本文从岗位本质出发,系统拆解 AI 产品经理的职责、岗位类型与能力模型,帮助读者理解 AI PM 与传统 PM 的核心差异,明确自己适合的转型方向,并建立“对 AI 产品结果负责”的产品认知。

从零开始,掌握网络安全。提供全面的网络安全教程、攻防演练技巧和行业动态。 1016

【高并发系列】17、线程池那些事儿1

多线程软件设计方法可以最大限度地发挥现代多核处理器的计算能力,提高生产系统的吞吐量和性能; JDK提供的Execotur框架: 1、固定大小线程池newFixedThreadPool 5个固定大小的线程池,提交10个任务,分2批执行任务; public class ThreadPoolDemo { public static class MyTask implements Runn...

hellboy0621的博客 206

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

从根上理解高性能、高并发(一):深入计算机底层,理解线程与线程池

本文原题“聊聊TCP连接耗时的那些事儿”,本次收录已征得作者同意,转载请联系作者。有少许改动。 1、系列文章引言 1.1 文章目的 作为即时通讯技术的开发者来说,高性能、高并发相关的技术概念早就了然与胸,什么线程池、零拷贝、多路复用、事件驱动、epoll等等名词信手拈来,又或许你对具有这些技术特征的技术框架比如:Java的Netty、Php的workman、Go的nget等熟练掌握。但真正到了面视或者技术实践过程中遇到无法释怀的疑惑时,方知自已所掌握的不过是皮毛。 返璞归真、回归本质,这些技术特征背

hellojackjiang2011的博客 929

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【高并发系列】15、线程阻塞工具类 LockSupport

1、定时阻塞 java.util.concurrent.locks.LockSupport可以在线程内任意位置让线程阻塞; 与Thread.suspend()方法比,弥补了由于resume()方法在suspend()方法之前发生而导致线程无法继续执行的情况; 与Object.wait()方法比,不需要首先获得某个对象的锁,也不会抛出InterruptedException异常; 使用Loc...

hellboy0621的博客 278

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

浅谈 高并发 处理方案

分享一套我整理的面试干货,这份文档结合了我多年的面试官经验,站在面试官的角度来告诉你,面试官提的那些问题他最想听到你给他的回答是什么,分享出来帮助那些对前途感到迷茫的朋友。

m0_72758098的博客 1125

一步一步写线程之四简单线程池

写线程池不要着急,要把基础打好,然后多看看网上各路大神对线程池的看法和他们的线程池代码。当然,各种框架中的线程池的代码更有参考作用。多看多写,多探寻原因究竟,更要多引入新技术新知识。这样,线程池自然会写好。写好代码后,还要不断的回头完善和修改自己写的代码,这样,最终就会写出一个不错的线程池。与诸君共勉!

fpcc的专栏 1135

JAVA之秒杀那点事儿

在电商中经常会遇到秒杀/限时抢购等一系列高并发的应用场景.整理一下秒杀/抢购的设计全过程1.架构思路问题1:在高并发下,数据库的性能下降很大,mysql下降的特别严重,而且io操作会消耗大量的资源.这个时候我们选择使用基于内存的redis,redis的密集io.问题2:秒杀/限购系统是一个集群系统,在硬件不提升的情况下,使用nginx做负载均衡2.实现的难点超买超卖的解决订单持久化,多线程将订单写...

qq_36987761的博客 376

计算机专业名词那些事儿

这篇博客旨在用通俗易懂的话介绍计算机的专业名词。能够坚持多久我也不知道。但是能够坚持一个词算一个。以下的内容来自我阅读其他博主的资料和自己浅薄见解,如有不正确的希望及时指出,以免耽误别人。 1、并发 比并发更让人熟悉的词,可能是高并发;那么什么是并发呢? 例子: 早上起来出早饭,你妈给你准备了鸡蛋、粥、油条、豆浆 ··· ···· 嘎啦吧唧一堆;好的,接下来你就开始吃了。你先剥鸡蛋、剥了一下...

Kincae的博客 3109

高并发

推荐书籍:1.java编程思想 2.企业应用架构模式 3.java并发编程实战 并发编程基础篇 ​ 课程目录: ​ 1.线程安全基础知识、synchronized、volatile关键字的实际场景使用 ​ 2.线程之间通信wait、notify、ThreadLocal、单列和多线程 一、线程安全 ​ 线程安全概念:当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的 ​ synchronized:可以在任意对象及方法上加锁,而加锁的这段代码称

u011533553的博客 194

深入eXosip事件循环:从UAC/UAS示例代码看SIP状态机与线程安全那些事儿

本文深入探讨eXosip事件循环中的SIP状态机与线程安全问题,通过UAC/UAS示例代码分析常见陷阱与优化策略。文章详细介绍了动态超时机制、状态机管理最佳实践及线程安全操作指南,帮助开发者解决高并发场景下的SIP协议栈开发难题。

weixin_30872337的博客 500

CPU高速缓存那些事儿

引言 在分析JDK8新增的高并发原子累加器Striped64的时候,发现有一个“伪共享”的概念,而要理解它必须对CPU缓存有一定的了解,所以本文将先对CPU的缓存架构以及一些相关术语做一个研究探索。   CPU缓存的原理       众所周知,在如今的计算机时代,CPU的运算处理速度与内存读写速度的差异非常巨大,为了解决这种差异充分利用CPU的使用效率,CPU缓存应运而生,它是介于CP...

春花秋月何时了 1万+

Java 21虚拟线程在RAG服务中的实践:性能提升与架构演进

虚拟线程是Java并发编程的重要革新,它通过轻量级线程模型解决了传统平台线程在高并发I/O场景下的资源瓶颈问题。其核心原理在于由JVM管理线程生命周期,在遇到I/O阻塞时自动挂起并释放底层载体线程,从而允许开发者使用直观的同步编程模型获得异步性能。这一技术价值在于显著提升吞吐量、降低延迟,并简化代码复杂度。在检索增强生成(RAG)这类典型的高并发、高I/O等待的应用场景中,虚拟线程能够充分发挥优势,通过重构服务架构,将线程池管理转变为资源访问控制,实现性能的质的飞跃。本文基于实际项目经验,深入探讨了在RAG

weixin_33816300的博客 389

Redis 版本演进、新特性与协议那些事儿

Redis 每个版本都有自己的脾气,有的适合稳扎稳打,有的适合尝鲜,有的为了适应 AI。扒一扒每个版本的核心变化、好玩的新命令、协议许可证的来龙去脉。另外,如果你是自己下载 Redis 源码编译、或者用 Docker 跑在服务器上,那后面提到的那些许可证问题——基本跟你没啥关系。协议限制主要针对的是云厂商把 Redis 打包成托管服务卖钱。你自己用,该咋用还咋用。1.x–2.x:打基础,数据结构 + Lua 脚本3.x–5.x:分布式 + 流消息6.x:多线程 I/O + ACL7.x。

代码构筑系统,架构驱动未来 —— 一名深耕 Java 技术的实践者 562

C++高并发服务构建:从Reactor模式到性能调优实战

高并发网络编程的核心在于高效处理海量连接与I/O事件,其基础是I/O多路复用技术,如Linux的epoll。该技术通过单线程或少量线程监控大量文件描述符,仅在事件就绪时触发处理,避免了传统多线程模型因线程切换和内存开销导致的性能瓶颈。基于此原理,Reactor模式成为构建高性能网络服务的经典架构,它将事件循环、事件分发与业务处理解耦,实现了高吞吐与低延迟。这种模式在实时通信、在线游戏、金融交易等需要处理大量并发连接的场景中具有极高价值。本文以构建一个实时在线用户状态推送服务为例,深入剖析了如何从零实现一个基

cuxiong8996的博客 578
上一篇: 【高并发系列】2、JMM
下一篇: 【高并发系列】4、volatile
hellboy0621
博客等级 码龄9年 61粉丝 147原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值