vue3: pdf.js 3.4.120 using javascript

news2025/5/13 9:40:12
npm install pdfjs-dist@3.4.120

 项目结构:

pdfjsViewer.vue

<template>
  <div>
      <div v-if="loading" class="flex justify-center items-center py-8">
          <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-500"></div>
      </div>
      <div v-else>
          <div class="flex flex-col md:flex-row gap-4 mb-4">
              <button @click="loadPreviousPage" :disabled="currentPage === 1" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
                  <i class="fa fa-arrow-left mr-1"></i> 上一页
              </button>
              <button @click="loadFirstPage" :disabled="currentPage === 1" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
                  <i class="fa fa-fast-backward mr-1"></i> 第一页
              </button>
              <span class="text-lg self-center">
                  第 {{ currentPage }} 页,共 {{ totalPages }} 页
              </span>
              <button @click="loadNextPage" :disabled="currentPage === totalPages" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
                  下一页 <i class="fa fa-arrow-right ml-1"></i>
              </button>
              <button @click="loadLastPage" :disabled="currentPage === totalPages" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
                  最后一页 <i class="fa fa-fast-forward ml-1"></i>
              </button>
          </div>
      </div>
      <div class="flex items-center mb-4">
          <span class="mr-2">缩放:</span>
          <button @click="zoomOut" class="px-2 py-1 bg-gray-200 rounded hover:bg-gray-300">
              <i class="fa fa-search-minus"></i>
          </button>
          <span class="mx-2">{{ Math.round(scale * 100) }}%</span>
          <button @click="zoomIn" class="px-2 py-1 bg-gray-200 rounded hover:bg-gray-300">
              <i class="fa fa-search-plus"></i>
          </button>
          <select v-model="scaleValue" @change="changeScale" class="ml-4 px-2 py-1 border rounded">
              <option value="0.5">50%</option>
              <option value="0.75">75%</option>
              <option value="1">100%</option>
              <option value="1.25">125%</option>
              <option value="1.5">150%</option>
              <option value="2">200%</option>
          </select>
      </div>  
      <div class="relative w-full bg-gray-100" ref="pdfContainer">
          <canvas ref="pdfCanvas" class="border-2 border-gray-300 rounded shadow-lg w-full"></canvas>
      </div>
  </div>
</template>
 
<script>
// 调整导入路径以适应3.4.120版本
import * as pdfjsLib from 'pdfjs-dist/build/pdf';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker';
 
// 设置worker路径
pdfjsLib.GlobalWorkerOptions.workerSrc = '/node_modules/pdfjs-dist/build/pdf.worker.js';
 
export default {
  name: 'PdfViewer',
  props: {
      pdfPath: {
          type: [String, Uint8Array],
          required: true
      }
  },
  data() {
      return {
          //pdfDoc: null,
          currentPage: 1,
          totalPages: 0,
          scale: 1,
          scaleValue: '1',
          canvas: null,
          ctx: null,
          renderTask: null,
          loading: true,
          error: null
      };
  },
  watch: {
      pdfPath: {
          immediate: true,
          handler(newVal) {
              this.loadPDF(newVal);
          }
      }
  },
  mounted() {
      this.$nextTick(() => {
          this.canvas = this.$refs.pdfCanvas;
          this.ctx = this.canvas.getContext('2d');
      });
  },
  methods: {
      async loadPDF(path) {
          this.loading = true;
          this.error = null;
          this.pdfDoc= null;
 
          try {
              await this.$nextTick();
               
              if (!this.$refs.pdfCanvas) {
                  console.error('Canvas元素未找到');
                  this.error = 'Canvas元素未找到';
                  return;
              }
               
              this.canvas = this.$refs.pdfCanvas;
              this.ctx = this.canvas.getContext('2d');
               
              if (this.renderTask) {
                  //this.renderTask.cancel();
              }
 
              this.currentPage = 1;
 
              const loadingTask = pdfjsLib.getDocument({
                  url: path,
                  disableFontFace: false,
                  cMapUrl: '/node_modules/pdfjs-dist/cmaps/',
                  cMapPacked: true
              });
 
              this.pdfDoc = await loadingTask.promise;
              this.totalPages = this.pdfDoc.numPages;
               
              console.log('PDF加载成功,总页数:', this.totalPages);
              await this.renderPage(this.currentPage);
               
              this.$emit('pageChanged', this.currentPage, this.totalPages);
          } catch (error) {
              console.error('加载PDF时出错:', error);
              this.error = error.message;
              alert('加载PDF失败: ' + error.message);
          } finally {
              this.loading = false;
          }
      },
 
      async renderPage(num) {
          if (!this.pdfDoc || !this.canvas || !this.ctx) {
              console.error('PDF文档或Canvas未初始化');
              return;
          }
 
          try {
 
              await this.$nextTick();
              console.log("renderPage:");
              console.log(this.pdfDoc);
              console.log('渲染第', num, '页,缩放比例:', this.scale);
               
              if (this.renderTask) {
                  //this.renderTask.cancel();
              }
 
              const page = await this.pdfDoc.getPage(num);
               console.log("pp",page);
              const viewport = page.getViewport({ scale: this.scale });
               
              // 设置canvas尺寸
              this.canvas.height = viewport.height;
              this.canvas.width = viewport.width;
               
              // 清除canvas
              this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
 
              console.log('Canvas尺寸设置为:', this.canvas.width, 'x', this.canvas.height);
 
              const renderContext = {
                  canvasContext: this.ctx,
                  viewport: viewport
              };
 
              console.log('开始渲染页面...');
              this.renderTask = page.render(renderContext);
 
              await this.renderTask.promise;
 
              console.log('页面渲染成功');
 
              this.$emit('pageChanged', num, this.totalPages);
          } catch (error) {
              if (error.name !== 'RenderingCancelledException') {
                  console.error('渲染页面时出错:', error);
                  this.error = error.message;
              }
          }
      },
      async loadFirstPage() {
          if (this.currentPage !== 1) {
              this.currentPage = 1;
              console.log("Page:");
              console.log(this.pdfDoc);
              await this.renderPage(this.currentPage);
          }
      },
      async loadPreviousPage() {
          if (this.currentPage > 1) {
              this.currentPage--;
              console.log("Page:");
              console.log(this.pdfDoc);
              await this.renderPage(this.currentPage);
          }
      },
      async loadNextPage() {
          if (this.currentPage < this.totalPages) {
              this.currentPage++;
              console.log("Page:");
              console.log(this.pdfDoc);
              await this.renderPage(this.currentPage);
          }
      },
      async loadLastPage() {
          if (this.currentPage !== this.totalPages) {
              this.currentPage = this.totalPages;
              console.log("Page:");
              console.log(this.pdfDoc);
              await this.renderPage(this.currentPage);
          }
      },
      zoomIn() {
          this.scale += 0.25;
          this.scaleValue = this.scale.toString();
          this.renderPage(this.currentPage);
      },
      zoomOut() {
          if (this.scale > 0.5) {
              this.scale -= 0.25;
              this.scaleValue = this.scale.toString();
              this.renderPage(this.currentPage);
          }
      },
      changeScale() {
          this.scale = parseFloat(this.scaleValue);
          this.renderPage(this.currentPage);
      }
  }
};
</script>
 
<style scoped>
.pdf-container {
  overflow: auto;
  min-height: 500px;
}
canvas {
  max-width: 100%;
  display: block;
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>

dupdf.vue

<!--
 * ......................................&&.........................
 * ....................................&&&..........................
 * .................................&&&&............................
 * ...............................&&&&..............................
 * .............................&&&&&&..............................
 * ...........................&&&&&&....&&&..&&&&&&&&&&&&&&&........
 * ..................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&..............
 * ................&...&&&&&&&&&&&&&&&&&&&&&&&&&&&&.................
 * .......................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.........
 * ...................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...............
 * ..................&&&   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&............
 * ...............&&&&&@  &&&&&&&&&&..&&&&&&&&&&&&&&&&&&&...........
 * ..............&&&&&&&&&&&&&&&.&&....&&&&&&&&&&&&&..&&&&&.........
 * ..........&&&&&&&&&&&&&&&&&&...&.....&&&&&&&&&&&&&...&&&&........
 * ........&&&&&&&&&&&&&&&&&&&.........&&&&&&&&&&&&&&&....&&&.......
 * .......&&&&&&&&.....................&&&&&&&&&&&&&&&&.....&&......
 * ........&&&&&.....................&&&&&&&&&&&&&&&&&&.............
 * ..........&...................&&&&&&&&&&&&&&&&&&&&&&&............
 * ................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&............
 * ..................&&&&&&&&&&&&&&&&&&&&&&&&&&&&..&&&&&............
 * ..............&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&....&&&&&............
 * ...........&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&......&&&&............
 * .........&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.........&&&&............
 * .......&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...........&&&&............
 * ......&&&&&&&&&&&&&&&&&&&...&&&&&&...............&&&.............
 * .....&&&&&&&&&&&&&&&&............................&&..............
 * ....&&&&&&&&&&&&&&&.................&&...........................
 * ...&&&&&&&&&&&&&&&.....................&&&&......................
 * ...&&&&&&&&&&.&&&........................&&&&&...................
 * ..&&&&&&&&&&&..&&..........................&&&&&&&...............
 * ..&&&&&&&&&&&&...&............&&&.....&&&&...&&&&&&&.............
 * ..&&&&&&&&&&&&&.................&&&.....&&&&&&&&&&&&&&...........
 * ..&&&&&&&&&&&&&&&&..............&&&&&&&&&&&&&&&&&&&&&&&&.........
 * ..&&.&&&&&&&&&&&&&&&&&.........&&&&&&&&&&&&&&&&&&&&&&&&&&&.......
 * ...&&..&&&&&&&&&&&&.........&&&&&&&&&&&&&&&&...&&&&&&&&&&&&......
 * ....&..&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...........&&&&&&&&.....
 * .......&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&..............&&&&&&&....
 * .......&&&&&.&&&&&&&&&&&&&&&&&&..&&&&&&&&...&..........&&&&&&....
 * ........&&&.....&&&&&&&&&&&&&.....&&&&&&&&&&...........&..&&&&...
 * .......&&&........&&&.&&&&&&&&&.....&&&&&.................&&&&...
 * .......&&&...............&&&&&&&.......&&&&&&&&............&&&...
 * ........&&...................&&&&&&.........................&&&..
 * .........&.....................&&&&........................&&....
 * ...............................&&&.......................&&......
 * ................................&&......................&&.......
 * .................................&&..............................
 * ..................................&..............................
 *
 * @Author: geovindu
 * @Date: 2025-05-12 19:12:10
 * @LastEditors: geovindu
 * @LastEditTime: 2025-05-12 19:17:45
 * @FilePath: \vue\vuejs\src\components\dupdf.vue
 * @Description: geovindu
 * @lib,packpage:
 *
 * @IDE: vscode
 * @jslib: node 20 vue.js 3.0
 * @OS: windows10
 * @database: mysql 8.0  sql server 2019 postgreSQL 16
 * Copyright (c) geovindu 2025 by geovindu@163.com, All Rights Reserved.
 -->
 
<template>
  <div class="container mx-auto p-4">
      <h1 class="text-2xl font-bold mb-4">PDF查看器</h1>
      <PdfViewer :pdfPath="pdfPath" @pageChanged="handlePageChanged" />
      <div class="mt-4">
          <input type="file" @change="handleFileSelect" accept=".pdf" class="border p-2 rounded">
      </div>
  </div>
</template>
 
<script>
import PdfViewer from './pdfjsViewer.vue';
 
export default {
  name: 'App',
  components: {
      PdfViewer
  },
  data() {
      return {
          pdfPath: './09.pdf'
      };
  },
  methods: {
      handlePageChanged(currentPage, totalPages) {
          console.log(`当前页码: ${currentPage}, 总页数: ${totalPages}`);
          // 可以在这里更新父组件状态或执行其他操作
      },
      async handleFileSelect(e) {
          const file = e.target.files[0];
          if (file) {
              const blob = new Blob([file], { type: file.type });
              this.pdfPath = URL.createObjectURL(blob);
          }
      }
  }
};
</script>

App.vue

<!--
 *
 *   ┏┓   ┏┓+ +
 *  ┏┛┻━━━┛┻┓ + +
 *  ┃       ┃  
 *  ┃   ━   ┃ ++ + + +
 *  ████━████ ┃+
 *  ┃       ┃ +
 *  ┃   ┻   ┃
 *  ┃       ┃ + +
 *  ┗━┓   ┏━┛
 *    ┃   ┃           
 *    ┃   ┃ + + + +
 *    ┃   ┃
 *    ┃   ┃ +  神兽保佑
 *    ┃   ┃    代码无bug  
 *    ┃   ┃  +         
 *    ┃    ┗━━━┓ + +
 *    ┃        ┣┓
 *    ┃        ┏┛
 *    ┗┓┓┏━┳┓┏┛ + + + +
 *     ┃┫┫ ┃┫┫
 *     ┗┻┛ ┗┻┛+ + + +
 *
 *
 * @Author: geovindu
 * @Date: 2024-08-05 08:57:12
 * @LastEditors: geovindu
 * @LastEditTime: 2024-10-16 09:20:45
 * @FilePath: \vue\vuejs\src\App.vue
 * @Description: geovindu
 * @lib,packpage:
 *
 * @IDE: vscode
 * @jslib: node 20 vue.js 3.0
 * @OS: windows10
 * @database: mysql 8.0  sql server 2019 postgreSQL 16
 * Copyright (c) geovindu 2024 by geovindu@163.com, All Rights Reserved.
 -->
 
<script setup>
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
</script>
 
<template>
  <header>
    <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />
 
    <div class="wrapper">
      <HelloWorld msg="You did it!" />
 
      <nav>
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about">About</RouterLink>
        <RouterLink to="/doc">Word</RouterLink>
        <RouterLink to="/excel">Excel</RouterLink>
        <RouterLink to="/pdf">Pdf</RouterLink>
        <RouterLink to="/pdfjs">Pdfjs</RouterLink>
        <RouterLink to="/dupdfjs">duPdfjs</RouterLink>
      </nav>
    </div>
  </header>
 
  <RouterView />
</template>
 
<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}
 
.logo {
  display: block;
  margin: 0 auto 2rem;
}
 
nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}
 
nav a.router-link-exact-active {
  color: var(--color-text);
}
 
nav a.router-link-exact-active:hover {
  background-color: transparent;
}
 
nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}
 
nav a:first-of-type {
  border: 0;
}
 
@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }
 
  .logo {
    margin: 0 2rem 0 0;
  }
 
  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
 
  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
 
    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>

word,excel,ppt 在线预览暂不示例

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2374560.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

想实现一个基于MCP的pptx生成系统架构图【初版实现】

技术栈:Python + MCP协议 + python-pptx + FastMCP 核心创新点:通过MCP协议实现PPTX元素的动态化生成与标准化模板管理 当前还是个半成品,后续持续更新。 主要先介绍一下思路。 一、MCP协议与系统设计原理 1.1 为什么选择MCP? 标准化工具调用:通过MCP将PPTX元素生成逻辑封…

PyTorch Lightning实战 - 训练 MNIST 数据集

MNIST with PyTorch Lightning 利用 PyTorch Lightning 训练 MNIST 数据。验证梯度范数、学习率、优化器对训练的影响。 pip show lightning Version: 2.5.1.post0Fast dev run DATASET_DIR"/repos/datasets" python mnist_pl.py --output_grad_norm --fast_dev_run…

力扣2094题解

记录&#xff1a; 2025.5.12 题目&#xff1a; 思路&#xff1a; 暴力遍历。 解题步骤&#xff1a; 1.统计数字出现次数&#xff1a;使用数组cnt来记录输入数组中每个数字的出现次数。 2.生成三位偶数&#xff1a;通过循环从100开始&#xff0c;每次递增2&#xff0c;生成…

DHCP自动分配IP

DHCP自动分配IP 练习1 路由器 Router>en Router#conf t Router(config)#ip dhcp pool ip10 //创建DHCP地址池 Router(dhcp-config)#network 192.168.20.0 255.255.255.0 // 配置网络地址和子网掩码 Router(dhcp-config)#default-router 192.168.20.254 //配置默认网关 Rou…

【CF】Day57——Codeforces Round 955 (Div. 2, with prizes from NEAR!) BCD

B. Collatz Conjecture 题目&#xff1a; 思路&#xff1a; 简单模拟 很简单的模拟&#xff0c;我们只需要快速的找到下一个离 x 最近的 y 的倍数即可&#xff08;要大于 x&#xff09; 这里我们可以这样写 add y - (x % y)&#xff0c;这样就知道如果 x 要变成 y 的倍数还要…

(done) 补充:xv6 的一个用户程序 init 是怎么启动的 ?它如何启动第一个 bash ?

先看 main.c 从函数名来看&#xff0c;比较相关的就 userinit() 和 scheduler() #include "types.h" #include "param.h" #include "memlayout.h" #include "riscv.h" #include "defs.h"volatile static int started 0;//…

超详细讲解C语言转义字符\a \b \r \t \? \n等等

转义字符 C语言有一组字符很特殊&#xff0c;叫做转义字符&#xff0c;顾名思义&#xff0c;改变原来的意思的字符。 1 \? ??)是一个三字母词&#xff0c;在以前的编译器它会被编译为] (??会被编译为[ 因此在以前输入(are you ok ??)就会被编译为are you ok ] 解决这个…

SpringBoot校园失物招领信息平台

SpringBoot校园失物招领信息平台 文章目录 SpringBoot校园失物招领信息平台1、技术栈2、项目说明2.1、登录注册2.2、管理员端截图2.3、用户端截图 3、核心代码实现3.1、前端首页3.2、前端招领广场3.3、后端业务处理 1、技术栈 本项目采用前后端分离的架构&#xff0c;前端和后…

rust 全栈应用框架dioxus server

接上一篇文章dioxus全栈应用框架的基本使用&#xff0c;支持web、desktop、mobile等平台。 可以先查看上一篇文章rust 全栈应用框架dioxus&#x1f448; 既然是全栈框架&#xff0c;那肯定是得有后端服务的&#xff0c;之前创建的服务没有包含后端服务包&#xff0c;我们修改…

西安交大多校联训NOIP1模拟赛题解

西安交大多校联训NOIP1模拟赛题解 T1 秘境形式化题意思路代码&#xff08;丑陋&#xff09; T2 礼物形式化题意思路代码&#xff08;实现&#xff09; T3 小盒子的数论形式化题意思路代码&#xff08;分讨&#xff09; T4 猫猫贴贴(CF997E)形式化题意思路代码&#xff08;深奥&…

数据结构(三)——栈和队列

一、栈和队列的定义和特点 栈&#xff1a;受约束的线性表&#xff0c;只允许栈顶元素入栈和出栈 对栈来说&#xff0c;表尾端称为栈顶&#xff0c;表头端称为栈底&#xff0c;不含元素的空表称为空栈 先进后出&#xff0c;后进先出 队列&#xff1a;受约束的线性表&#xff0…

若依定制pdf生成实战

一、介绍 使用 Java Apache POI 将文字渲染到 Word 模板是一种常见的文档自动化技术&#xff0c;广泛应用于批量生成或定制 Word 文档的场景。使用aspose可以将word转成pdf从而达到定制化pdf的目的。 参考文档&#xff1a;java实现Word转Pdf&#xff08;Windows、Linux通用&a…

c++STL-vector的模拟实现

cSTL-vector的模拟实现 vector的模拟实现基本信息构造函数析构函数返回容量&#xff08;capacity&#xff09;返回元素个数&#xff08;size&#xff09;扩容&#xff08;reserve和resize&#xff09;访问&#xff08;[]&#xff09;迭代器&#xff08;**iterator**&#xff09…

在 Elasticsearch 中连接两个索引

作者&#xff1a;来自 Elastic Kofi Bartlett 解释如何使用 terms query 和 enrich processor 来连接 Elasticsearch 中的两个索引。 更多有关连接两个索引的查询&#xff0c;请参阅文章 “Elastic&#xff1a;开发者上手指南” 中的 “丰富数据及 lookup” 章节。 Elasticsea…

使用 Watt toolkit 加速 git clone

一、前言 Watt toolkit 工具是我经常用于加速 GitHub 网页和 Steam 游戏商店访问的工具&#xff0c;最近想加速 git clone&#xff0c;发现可以使用 Watt toolkit 工具的代理实现。 二、查看端口 我这里以 Ubuntu 为例&#xff0c;首先是需要将加速模式设置为 System&#xff1…

应急响应靶机——WhereIS?

用户名及密码&#xff1a;zgsf/zgsf 下载资源还有个解题.exe: 1、攻击者的两个ip地址 2、flag1和flag2 3、后门程序进程名称 4、攻击者的提权方式(输入程序名称即可) 之前的命令&#xff1a; 1、攻击者的两个ip地址 先获得root权限&#xff0c;查看一下历史命令记录&#x…

Docke容器下JAVA系统时间与Linux服务器时间不一致问题解决办法

本篇文章主要讲解&#xff0c;通过docker部署jar包运行环境后出现java系统内时间与服务器、个人电脑真实时间不一致的问题原因及解决办法。 作者&#xff1a;任聪聪 日期&#xff1a;2025年5月12日 问题现象&#xff1a; 说明&#xff1a;与实际时间不符&#xff0c;同时与服务…

【MCP】其他MCP服务((GitHub)

【MCP】其他MCP服务&#xff08;&#xff08;GitHub&#xff09; 1、其他MCP服务&#xff08;GitHub&#xff09; MCP广场&#xff1a;https://www.modelscope.cn/mcp 1、其他MCP服务&#xff08;GitHub&#xff09; 打开MCP广场 找到github服务 访问github生成令牌 先…

内存 -- Linux内核内存分配机制

内存可以怎么用&#xff1f; kmalloc&#xff1a;内核最常用&#xff0c;用于频繁使用的小内存申请 alloc_pages&#xff1a;以页框为单位申请&#xff0c;物理内存连续 vmalloc&#xff1a;虚拟地址连续的内存块&#xff0c;物理地址不连线 dma_alloc_coherent&#xff1a;常…

关于读写锁的一些理解

同一线程的两种情况&#xff1a; 读读&#xff1a; public static void main(String[] args) throws InterruptedException {ReentrantReadWriteLock lock new ReentrantReadWriteLock();Lock readLock lock.readLock();Lock writeLock lock.writeLock();readLock.lock();S…