2026-02-18 15:52:55 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="process-demo">
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="controls-section">
|
|
|
|
|
|
<button class="action-btn" :class="{ active: isRunning }" @click="toggleSimulation">
|
|
|
|
|
|
{{ isRunning ? '⏸ 暂停时间片轮转' : '▶️ 启动 CPU' }}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<div class="speed-control">
|
|
|
|
|
|
<label>时间流速:</label>
|
|
|
|
|
|
<button :class="{ active: speed === 'slow' }" @click="setSpeed('slow')">极慢动作</button>
|
|
|
|
|
|
<button :class="{ active: speed === 'fast' }" @click="setSpeed('fast')">真实速度</button>
|
|
|
|
|
|
</div>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="cpu-container">
|
|
|
|
|
|
<div class="cpu-core" :class="{ active: isRunning }">
|
|
|
|
|
|
<div class="cpu-title">单核 CPU</div>
|
|
|
|
|
|
<div class="current-task">
|
|
|
|
|
|
<span v-if="activeProcess" class="task-badge">
|
|
|
|
|
|
正在处理: {{ activeProcess.icon }} {{ activeProcess.name }}
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</span>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<span v-else class="task-badge idle">
|
|
|
|
|
|
空闲中...
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<!-- 连接线动画 -->
|
|
|
|
|
|
<div class="connector">
|
|
|
|
|
|
<div
|
|
|
|
|
|
class="data-flow"
|
|
|
|
|
|
:class="[ `flow-${activeProcessId}`, { running: isRunning }]">
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="processes-grid">
|
|
|
|
|
|
<div
|
|
|
|
|
|
v-for="p in processes"
|
|
|
|
|
|
:key="p.id"
|
|
|
|
|
|
class="process-card"
|
|
|
|
|
|
:class="{ active: p.id === activeProcessId }"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="p-header">
|
|
|
|
|
|
<div class="p-title">
|
|
|
|
|
|
<span class="icon">{{ p.icon }}</span>
|
|
|
|
|
|
<span class="name">{{ p.name }}</span>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<span class="status-badge" :class="p.id === activeProcessId ? 'running' : 'waiting'">
|
|
|
|
|
|
{{ p.id === activeProcessId ? '独占 CPU' : '排队等待' }}
|
|
|
|
|
|
</span>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="p-progress">
|
|
|
|
|
|
<div class="progress-track">
|
|
|
|
|
|
<div class="progress-fill" :style="{ width: p.progress + '%' }"></div>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="progress-text">{{ Math.floor(p.progress) }}% 完成</div>
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
<div class="explanation-box" :class="{ show: isRunning && speed === 'fast' }">
|
|
|
|
|
|
💡 **关键启示**:当切换速度足够快时,肉眼已经无法分辨谁在“等待”。这也就是为什么只有一个 CPU 核心的电脑,依然能让你一边听歌一边打字!
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-02-23 01:40:56 +08:00
|
|
|
|
import { ref, computed, onUnmounted } from 'vue'
|
2026-02-18 15:52:55 +08:00
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
const isRunning = ref(false)
|
|
|
|
|
|
const activeProcessId = ref(null)
|
|
|
|
|
|
const speed = ref('slow')
|
|
|
|
|
|
let interval = null
|
2026-02-18 15:52:55 +08:00
|
|
|
|
|
|
|
|
|
|
const processes = ref([
|
2026-02-23 01:40:56 +08:00
|
|
|
|
{ id: 1, name: '微信接收', icon: '💬', progress: 0 },
|
|
|
|
|
|
{ id: 2, name: '音乐播放', icon: '🎵', progress: 0 },
|
|
|
|
|
|
{ id: 3, name: '游戏渲染', icon: '🎮', progress: 0 }
|
2026-02-18 15:52:55 +08:00
|
|
|
|
])
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
const activeProcess = computed(() => processes.value.find(p => p.id === activeProcessId.value))
|
|
|
|
|
|
|
|
|
|
|
|
const setSpeed = (s) => {
|
|
|
|
|
|
speed.value = s
|
|
|
|
|
|
if (isRunning.value) {
|
|
|
|
|
|
clearInterval(interval)
|
|
|
|
|
|
startLoop()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-18 15:52:55 +08:00
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
const startLoop = () => {
|
|
|
|
|
|
const switchTime = speed.value === 'slow' ? 1200 : 80; // 慢动作 1.2s,快动作极快
|
|
|
|
|
|
|
|
|
|
|
|
if (!activeProcessId.value) {
|
|
|
|
|
|
activeProcessId.value = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interval = setInterval(() => {
|
|
|
|
|
|
// 增加当前进度
|
|
|
|
|
|
const curr = processes.value.find(p => p.id === activeProcessId.value)
|
|
|
|
|
|
if (curr) {
|
|
|
|
|
|
curr.progress += (speed.value === 'slow' ? 15 : 4)
|
|
|
|
|
|
if (curr.progress >= 100) curr.progress = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切换下一个
|
|
|
|
|
|
let nextId = activeProcessId.value + 1
|
|
|
|
|
|
if (nextId > 3) nextId = 1
|
|
|
|
|
|
activeProcessId.value = nextId
|
|
|
|
|
|
|
|
|
|
|
|
}, switchTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const toggleSimulation = () => {
|
|
|
|
|
|
if (isRunning.value) {
|
|
|
|
|
|
clearInterval(interval)
|
|
|
|
|
|
isRunning.value = false
|
|
|
|
|
|
activeProcessId.value = null
|
|
|
|
|
|
} else {
|
|
|
|
|
|
isRunning.value = true
|
|
|
|
|
|
startLoop()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
|
if (interval) clearInterval(interval)
|
|
|
|
|
|
})
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.process-demo {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-23 01:40:56 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
|
padding: 1.5rem;
|
|
|
|
|
|
margin: 1.5rem 0;
|
|
|
|
|
|
font-family: var(--vp-font-family-base);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.controls-section {
|
2026-02-18 15:52:55 +08:00
|
|
|
|
display: flex;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
justify-content: space-between;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
align-items: center;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
margin-bottom: 2rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
flex-wrap: wrap;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
gap: 1rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.action-btn {
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
padding: 0.6rem 1.2rem;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: all 0.3s ease;
|
|
|
|
|
|
min-width: 160px;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.action-btn.active {
|
|
|
|
|
|
background: var(--vp-c-danger-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
.action-btn:hover {
|
|
|
|
|
|
filter: brightness(1.1);
|
|
|
|
|
|
transform: translateY(-1px);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.speed-control {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
gap: 0.5rem;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
font-size: 0.9rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
.speed-control button {
|
|
|
|
|
|
background: transparent;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
padding: 0.3rem 0.8rem;
|
|
|
|
|
|
border-radius: 6px;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: all 0.2s;
|
|
|
|
|
|
}
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.speed-control button.active {
|
2026-02-18 15:52:55 +08:00
|
|
|
|
background: var(--vp-c-brand-soft);
|
2026-02-23 01:40:56 +08:00
|
|
|
|
color: var(--vp-c-brand-1);
|
|
|
|
|
|
border-color: var(--vp-c-brand-1);
|
|
|
|
|
|
font-weight: bold;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.cpu-container {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
margin-bottom: 2rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.cpu-core {
|
|
|
|
|
|
width: 240px;
|
|
|
|
|
|
height: 90px;
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
border: 2px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
|
|
|
|
|
|
transition: all 0.3s;
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
}
|
|
|
|
|
|
.cpu-core.active {
|
|
|
|
|
|
border-color: var(--vp-c-brand-1);
|
|
|
|
|
|
box-shadow: 0 0 20px var(--vp-c-brand-soft);
|
|
|
|
|
|
}
|
|
|
|
|
|
.cpu-title {
|
|
|
|
|
|
font-weight: 800;
|
|
|
|
|
|
font-size: 1.1rem;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
margin-bottom: 0.5rem;
|
|
|
|
|
|
letter-spacing: 2px;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.current-task {
|
|
|
|
|
|
height: 28px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
.task-badge {
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
color: white;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
padding: 0.2rem 0.8rem;
|
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
}
|
|
|
|
|
|
.task-badge.idle {
|
|
|
|
|
|
background: var(--vp-c-text-3);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
/* 连接线动画占位,简化效果,用发亮的虚线替代 */
|
|
|
|
|
|
.connector {
|
|
|
|
|
|
width: 2px;
|
|
|
|
|
|
height: 30px;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
background: var(--vp-c-divider);
|
2026-02-23 01:40:56 +08:00
|
|
|
|
margin-top: 5px;
|
|
|
|
|
|
position: relative;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.processes-grid {
|
|
|
|
|
|
display: grid;
|
|
|
|
|
|
grid-template-columns: repeat(3, 1fr);
|
|
|
|
|
|
gap: 1rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
@media (max-width: 640px) {
|
|
|
|
|
|
.processes-grid {
|
|
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
|
}
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.process-card {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
|
padding: 1rem;
|
|
|
|
|
|
transition: all 0.3s;
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
overflow: hidden;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.process-card.active {
|
|
|
|
|
|
border-color: var(--vp-c-brand-1);
|
|
|
|
|
|
transform: translateY(-2px);
|
|
|
|
|
|
box-shadow: 0 6px 16px var(--vp-c-brand-soft);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.process-card.active::before {
|
|
|
|
|
|
content: '';
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
top: 0; left: 0; right: 0;
|
|
|
|
|
|
height: 4px;
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.p-header {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
margin-bottom: 1rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.p-title {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 0.4rem;
|
|
|
|
|
|
font-weight: 600;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.status-badge {
|
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
|
padding: 0.1rem 0.5rem;
|
|
|
|
|
|
border-radius: 4px;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
font-weight: bold;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
.status-badge.waiting {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
.status-badge.running {
|
|
|
|
|
|
background: rgba(16, 185, 129, 0.15);
|
|
|
|
|
|
color: var(--vp-c-success-1);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.p-progress {
|
2026-02-18 15:52:55 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
2026-02-23 01:40:56 +08:00
|
|
|
|
gap: 0.5rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.progress-track {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 8px;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
overflow: hidden;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.progress-fill {
|
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
background: var(--vp-c-brand-1);
|
|
|
|
|
|
transition: width 0.1s linear;
|
|
|
|
|
|
}
|
|
|
|
|
|
.process-card.active .progress-fill {
|
|
|
|
|
|
background: var(--vp-c-success-1);
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.progress-text {
|
|
|
|
|
|
font-size: 0.75rem;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-02-23 01:40:56 +08:00
|
|
|
|
text-align: right;
|
|
|
|
|
|
font-variant-numeric: tabular-nums;
|
2026-02-18 15:52:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-23 01:40:56 +08:00
|
|
|
|
.explanation-box {
|
|
|
|
|
|
margin-top: 1.5rem;
|
|
|
|
|
|
padding: 1rem;
|
|
|
|
|
|
background: rgba(16, 185, 129, 0.1);
|
|
|
|
|
|
border-left: 4px solid var(--vp-c-success-1);
|
|
|
|
|
|
border-radius: 0 8px 8px 0;
|
|
|
|
|
|
font-size: 0.95rem;
|
|
|
|
|
|
opacity: 0;
|
|
|
|
|
|
transform: translateY(10px);
|
|
|
|
|
|
transition: all 0.5s ease;
|
|
|
|
|
|
}
|
|
|
|
|
|
.explanation-box.show {
|
|
|
|
|
|
opacity: 1;
|
|
|
|
|
|
transform: translateY(0);
|
|
|
|
|
|
}
|
2026-02-18 15:52:55 +08:00
|
|
|
|
</style>
|