color highlighter在sublime Text3中无法使用 5C

我的sublime text版本是3143,
安装color highlighter怎么配置都没有办法显示代码颜色,color highlighter版本是8.09,
下面是打开Preference –> Package Settings –> Color Highlighter –> Settings User的配置信息,
即使我所有都开成true,依然在css中不能高亮颜色。
{
"experimental":
{
"asynchronosly_update_color_scheme": true
},
"search_colors_in":
{
"all_content":
{
"color_highlighters":
{
"color_scheme":
{
"enabled": true,
"highlight_style": "filled"
},
"gutter_icons":
{
"enabled": true,
"icon_style": "circle"
},
"phantoms":
{
"enabled": true,
"length": 2,
"style": "right"
}
},
"enabled": true
},
"hover":
{
"color_highlighters":
{
"color_scheme":
{
"enabled": true,
"highlight_style": "filled"
},
"gutter_icons":
{
"enabled": true,
"icon_style": "circle"
},
"phantoms":
{
"enabled": true,
"length": 2,
"style": "right"
}
},
"enabled": true
},
"selection":
{
"color_highlighters":
{
"color_scheme":
{
"enabled": true,
"highlight_style": "filled"
},
"gutter_icons":
{
"enabled": true,
"icon_style": "circle"
},
"phantoms":
{
"enabled": true,
"length": 2,
"style": "right"
}
},
"enabled": true
}
}
}

1个回答

我也遇到同样问题。启动加载插件会报错,在控制台可以看见:
Output:

Error:
'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
Highlighting colors with gutter icons is not supported with current ImageMagick setup. Try configuring the "icon_factory setting"
Traceback (most recent call last):
File "F:\Linux\Sublime Text Build 3176 x64\sublime_plugin.py", line 298, in on_api_ready
plc()
File "F:\Linux\Sublime Text Build 3176 x64\Data\Installed Packages\Color Highlighter.sublime-package\main.py", line 606, in plugin_loaded
ColorHighlighterPlugin.init()
File "F:\Linux\Sublime Text Build 3176 x64\Data\Installed Packages\Color Highlighter.sublime-package\main.py", line 319, in init
ColorHighlighterPlugin._fake_color_scheme = ColorHighlighterPlugin.components.provide_fake_color_scheme()
File "F:\Linux\Sublime Text Build 3176 x64\Data\Installed Packages\Color Highlighter.sublime-package\main.py", line 197, in provide_fake_color_scheme
return self.provide_fake_color_scheme_data()[0]
File "F:\Linux\Sublime Text Build 3176 x64\Data\Installed Packages\Color Highlighter.sublime-package\main.py", line 188, in provide_fake_color_scheme_data
self._fake_color_scheme_data = parse_color_scheme(self.provide_color_scheme(), self._settings.debug)
File "F:\Linux\Sublime Text Build 3176 x64\Data\Installed Packages\Color Highlighter.sublime-package\color_scheme.py", line 43, in parse_color_scheme
color_scheme_xml = ElementTree.fromstring(color_scheme_content)
File "./python3.3/xml/etree/ElementTree.py", line 1356, in XML
File "", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
Package Control: Skipping automatic upgrade, last run at 2019-04-28 12:30:23, next run at 2019-04-28 13:30:23 or after
Detected ASCII vs Undefined with 100% confidence
Detected ASCII vs Undefined with 100% confidence
reloading settings Packages/User/Preferences.sublime-settings

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
sublime text 3 Color Highlighter无法使用

![图片说明](https://img-ask.csdn.net/upload/201807/21/1532183715_32233.png) 安装了color highlighter插件,没有反应,求解

sublime text3的colorhighlighter没有作用,怎么解决?

![图片说明](https://img-ask.csdn.net/upload/201810/18/1539863053_281806.png) 如图,安装了colorhighlighter一点反应都没有。 都设置过tool-> colorhighter里面的选项了。 看过官网,百度过,都没有解决方案。

用于Sublime Text 3的PHP Live或Run Error Highlighter

<div class="post-text" itemprop="text"> <p>Hi everyone i am using Sublime Text 3.<br>I have search a lot for php live or Run time error highlighter when i am typing my php on sublime just like in Dreamweaver it highlights the line where the error is but i did not find anything.all i get is sublimelinter i have installed it but it did not work for me.Also i have check php syntax checker but it is to old only used for sublime text 2. <br> can anyone suggest me where i download it or install it using my package control it will be really helpful...</p> </div>

Lucene中的highlighter.getBestFragment 取值为空!

有一个内容较多的txt,当搜索的关键词在txt前面一些,highlighter.getBestFragment(analyzer, "content", document.get("content"));就可以取得内容,但是当搜索的关键词在txt中的位置在后面时,此时highlighter.getBestFragment(analyzer, "content", document.get("content"));的内容就为空了,这是什么原因呢,还有不管highlighter.getBestFragment(analyzer, "content", document.get("content"));是不是为空,显示结果都是查询到了数据,就是提取关键词附近的段落有问题。

Lucene中的highlighter.getBestFragment 取值为空

有一个内容较多的txt,当搜索的关键词在txt前面一些,highlighter.getBestFragment(analyzer, "content", document.get("content"));就可以取得内容,但是当搜索的关键词在txt中的位置在后面时,此时highlighter.getBestFragment(analyzer, "content", document.get("content"));的内容就为空了,这是什么原因呢,还有不管highlighter.getBestFragment(analyzer, "content", document.get("content"));是不是为空,显示结果都是查询到了数据,就是提取关键词附近的段落有问题。

lucene使用中设置结果高亮显示的问题

用下面的代码设置查询结果高亮显示,可结果`<font color='red'>`, `</font>`都给加到text的最前面了,就比如说我对“我爱中国”里的“中国”进行查询,返回的text是“`<font color='red'></font>`我爱中国”,为什么呢 代码如下 String text = doc.get(fields[2]); SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>", "</font>"); Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query1)); highlighter.setTextFragmenter(new SimpleFragmenter(text.length())); TokenStream tokenStream = luceneAnalyzer.tokenStream("", new StringReader(text)); String highLightText = highlighter.getBestFragment(tokenStream, text);

语法Highlighter随机放置新行(或者可能在每个符号后面)

<div class="post-text" itemprop="text"> <p>I am using a simple implementation of Syntax Highlighter for a blog written in CodeIgniter. Here is the link <a href="http://code.angad.sg/index.php/blog/test" rel="nofollow noreferrer">http://code.angad.sg/index.php/blog/test</a></p> <p>Not sure why is this happening. I am not pasting the source code for the file; you can do a Ctrl+U on the link.</p> <p>Thanks :)</p> </div>

Lucene的Highlighter如何才能将不分词直接索引的字段加高亮?

【背景】 用户要求输入文章的完整标题也能将该文章完整检索下来,如“商海里穿梭的鱼:偷懒的6条原则”。 【我的想法】 在设计时多加一个字段用于对文章的标题不分词直接索引,这样就能实现用户要求的功能。但由于要结合Highlighter将用户输入的短语用标签对<font></font>高亮显示,而Highlighter的getBestFragment()方法又必须要使用分词器或Token进行分词,这样把标题分词后就导致无法对整个文章标题加高亮了。 对于这个问题,虽然可以在做页面时给完整的文章标题可以手工添加高亮标签对<font></font>,但这样会带来一些麻烦,如代码变得很不协调,更糟糕的是可能会出现这样的情况:用户选择以文章的完整标题作为输入,且标题是“暧昧”,而“暧昧”本身就是一个分词单元,这样就会产生<font color='red'><font color='red'>暧昧</fong></font>的标签对嵌套(内层标签对是高亮器自动添加的,外层的则是在页面输出时手工添加的)。 请问对于这个问题,有什么比较好的解决方法呢?

lucene搜索后,前端HTML页面没有高亮显示

** 问题描述:** 项目是前后端分离,后端Java开发,前端纯HTML显示,查询出结果返回给前端后,前端还是原文输出 ** 前端结果:** ![图片说明](https://img-ask.csdn.net/upload/201708/24/1503547722_808288.png) ** 后端部分代码** /** * 设置关键字高亮 * @param query 查询对象 * @param list 设置高亮的内容列表 * @param fields 字段名 */ public List<T> keyWordsHighLight(BooleanQuery query, List<T> list, String... fields){ Analyzer analyzer = new IKAnalyzer(); /**自定义标注高亮文本标签*/ SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>"); Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(130)); for(T entity : list){ try { for (String field : fields){ if(ObjectUtil.isNotEmpty(field) && !"tag".equals(field)){ String text = ObjectUtil.replaceHtml((String)Reflections.invokeGetter(entity, field)); String desciption = highlighter.getBestFragment(analyzer,field,text); if(desciption!=null){ Reflections.invokeSetter(entity, fields[0], desciption); break; } Reflections.invokeSetter(entity, fields[0], ObjectUtil.abbr(text, 130)); } } } catch (IOException e) { e.printStackTrace(); } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } } return list; }

java lucene Highlighter 包哪里下的到

兄弟们 我用lucene 做全文检索 ,要用到高亮,没想到Highlighter 包这么难找,请各位帮我找找 ?老弟感激不尽

Lucene Highlighter 高亮词前后的词的个数?

highlighter.setTextFragmenter(new SimpleFragmenter(15));//高亮词前后15 个字?

用PHP替换带标记链接的单词

<div class="post-text" itemprop="text"> <p>I have a text (<code>$text</code>) and an array of words (<code>$tags</code>). These words in the text should be replaced with links to other pages so they don't break the existing links in the text. In CakePHP there is a method in TextHelper for doing this but it is corrupted and it breaks the existing HTML links in the text. The method suppose to work like this:</p> <pre><code>$text=Text-&gt;highlight($text,$tags,'&lt;a href="/tags/\1"&gt;\1&lt;/a&gt;',1); </code></pre> <p>Below there is existing code in CakePHP TextHelper:</p> <pre><code>function highlight($text, $phrase, $highlighter = '&lt;span class="highlight"&gt;\1&lt;/span&gt;', $considerHtml = false) { if (empty($phrase)) { return $text; } if (is_array($phrase)) { $replace = array(); $with = array(); foreach ($phrase as $key =&gt; $value) { $key = $value; $value = $highlighter; $key = '(' . $key . ')'; if ($considerHtml) { $key = '(?![^&lt;]+&gt;)' . $key . '(?![^&lt;]+&gt;)'; } $replace[] = '|' . $key . '|ix'; $with[] = empty($value) ? $highlighter : $value; } return preg_replace($replace, $with, $text); } else { $phrase = '(' . $phrase . ')'; if ($considerHtml) { $phrase = '(?![^&lt;]+&gt;)' . $phrase . '(?![^&lt;]+&gt;)'; } return preg_replace('|'.$phrase.'|i', $highlighter, $text); } } </code></pre> </div>

我这个报了什么错?qt太难学了。

![图片说明](https://img-ask.csdn.net/upload/201601/21/1453378278_556120.jpg) ![图片说明](https://img-ask.csdn.net/upload/201601/21/1453378310_840289.jpg) ![图片说明](https://img-ask.csdn.net/upload/201601/21/1453378328_25098.jpg) ![图片说明](https://img-ask.csdn.net/upload/201601/21/1453378344_985474.jpg) ![图片说明](https://img-ask.csdn.net/upload/201601/21/1453378366_152511.jpg) ``` #include "mainwindow.h" #include "ui_mainwindow.h" #include <QTextFrame> #include <QDebug> #include <QLineEdit> #include <QDialog> #include <QPushButton> #include <QVBoxLayout> #include "mysyntaxhighlighter.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTextDocument *document=ui->textEdit->document(); QTextFrame *rootFrame=document->rootFrame(); QTextFrameFormat format; format.setBackground(Qt::blue); format.setBorderBrush(Qt::red); format.setBorder(3); rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; frameFormat.setBackground(Qt::lightGray); frameFormat.setMargin(20); frameFormat.setPadding(50); frameFormat.setBorder(6); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); QTextCursor cursor=ui->textEdit->textCursor(); cursor.insertFrame(frameFormat); QAction *action_textFrame=new QAction(tr("框架"),this); connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame())); ui->mainToolBar->addAction(action_textFrame); QAction *action_textBlock=new QAction(tr("文本块"),this); connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock())); ui->mainToolBar->addAction(action_textBlock); QAction *action_font=new QAction(tr("字体"),this); action_font->setCheckable(true); connect(action_font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool))); ui->mainToolBar->addAction(action_font); QAction *action_textFind=new QAction(tr("查找"),this); connect(action_textFind,SIGNAL(triggered()),this,SLOT(textFind())); ui->mainToolBar->addAction(action_textFind); highlighter=new MySyntaxHighlighter(ui->textEdit->document()); } void MainWindow::showTextFrame() { QTextDocument *document=ui->textEdit->document(); QTextFrame *frame=document->rootFrame(); QTextFrame::iterator it; for(it=frame->begin();it.atEnd()==0;++it) { QTextFrame *childFrame=it.currentFrame(); QTextBlock childBlock=it.currentBlock(); if(childFrame) qDebug()<<"frame"; else if(childBlock.isValid()) qDebug()<<"block:"<<childBlock.text(); } } void MainWindow::showTextBlock() { QTextDocument *document=ui->textEdit->document(); QTextBlock block=document->firstBlock(); qDebug()<<document->blockCount(); for(int i=0;i<document->blockCount();i++) { qDebug() <<tr("文本块%1,文本块首行行号为:%2,长度为:%3,内容为:").arg(i).arg(block.firstLineNumber()).arg(block.length()) <<block.text(); block=block.next(); } } void MainWindow::setTextFont(bool checked) { if(checked) { QTextCursor cursor=ui->textEdit->textCursor(); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); cursor.insertBlock(blockFormat); QTextCharFormat charFormat; charFormat.setBackground(Qt::lightGray); charFormat.setForeground(Qt::blue); charFormat.setFont(QFont(tr("宋体"),12,QFont::Bold,true)); charFormat.setFontUnderline(true); cursor.setCharFormat(charFormat); cursor.insertText(tr("测试字体")); } } void MainWindow::textFind() { QDialog *dlg=new QDialog(this); lineEdit=new QLineEdit(dlg); QPushButton *btn=new QPushButton(dlg); btn->setText(tr("查找下一个")); connect(btn,SIGNAL(clicked(bool)),this,SLOT(findNext())); QVBoxLayout *layout=new QVBoxLayout; layout->addWidget(lineEdit); layout->addWidget(btn); dlg->setLayout(layout); dlg->show(); } void MainWindow::findNext() { QString string=lineEdit->text(); bool isfind=ui->textEdit->find(string,QTextDocument::FindBackward); if(isfind) { qDebug()<<tr("行号:%1列号:%2").arg(ui->textEdit->textCursor().blockNumber()). arg(ui->textEdit->textCursor().columnNumber()); } } MainWindow::~MainWindow() { delete ui; } ```

使用geshi语法高亮显示Lua长字符串无法正确显示

<div class="post-text" itemprop="text"> <p>I am storing my new Lua code in to a Dokuwiki system which uses the geshi highlighter (written in php). How ever I am having a problem with long string definitions in lua for example</p> <pre><code>strLong = [[If this is a long string then I don't want it highlighted as code else it looks very silly]] </code></pre> <p>It is possible to correct this using the geshi lua.php file, I have already taken a quick look at the documentation at <a href="http://qbnz.com/highlighter/geshi-doc.html" rel="nofollow noreferrer">http://qbnz.com/highlighter/geshi-doc.html</a> and multiline comments are working fine it's just multiline strings which are not.</p> </div>

我的项目缺少哪个jar包呢?

<pre name="code" class="java">import org.apache.lucene.analysis.Analyzer; <span style="color: #000000;">import org.apache.lucene.document.Document;</span> <span style="color: #ff0000;"><span style="text-decoration: underline;">import org.apache.lucene.search.highlight.Highlighter;</span></span> 报红线的是缺少哪个jar包? </pre> <p> </p>

curl命令无法从api.github获取内容,但网络没问题

<div class="post-text" itemprop="text"> <p>I was trying use command <code>composer create-project laravel/laravel Laravel</code>, to create a new laravel project.</p> <p>But I got this in the terminal:</p> <pre><code>Failed to download jakub-onderka/php-console-color from dist: The "https://api.github.com/repos/JakubOnderka/PHP-Console-Color/zipball/e0b393dacf7703fc36a4efc3df1435485197e6c1" file could not be downloaded: failed to open stream: Connection timed out Now trying to download from source </code></pre> <p>Then, I tried this command:</p> <pre><code>curl https://api.github.com/repos/JakubOnderka/PHP-Console-Color/zipball/e0b393dacf7703fc36a4efc3df1435485197e6c1 </code></pre> <p>It cannot work.</p> <p>However, when I entered the address in the browser(FireFox), It works!</p> <p>What's wrong with that? Have suffered from this couple of days.</p> </div>

Java-Servlet线程安全问题

<p>在看zwchen很久之前的一篇博客,地址如下<br><br>http://zwchen.iteye.com/blog/91088<br><br>做了个测试,按照第一种做法</p> <p> </p> <p>为使用synchronized,<span style="color: #ff0000;"><strong>并未出现 线程共享情况</strong></span>,(我使用的是tomcat6.0带的servlet包)数据都是</p> <p> </p> <p>SimpleServlet@c4fe76 ==&gt; Thread[http-8080-1,5,main]: <br>Counter = 60<br>Counter = 61<br>Counter = 62<br>Counter = 63<br>Counter = 64<br>Counter = 65<br>Counter = 66<br>Counter = 67<br>Counter = 68<br>Counter = 69</p> <p> </p> <p><span style="font-family: Simsun; font-size: small;">SimpleServlet@c4fe76 ==&gt; Thread[http-8080-1,5,main]: <br>Counter = 70<br>Counter = 71<br>Counter = 72<br>Counter = 73<br>Counter = 74<br>Counter = 75<br>Counter = 76<br>Counter = 77<br>Counter = 78<br>Counter = 79</span></p> <p> </p> <p>SimpleServlet@c4fe76 ==&gt; Thread[http-8080-1,5,main]: <br>Counter = 80<br>Counter = 81<br>Counter = 82<br>Counter = 83<br>Counter = 84<br>Counter = 85<br>Counter = 86<br>Counter = 87<br>Counter = 88<br>Counter = 89</p> <p> </p> <p>正常输出。 请问各位高手,这个是为什么??</p> <p> </p> <p> </p> <p><span style="color: #ff0000;"><strong>还有,其中的</strong></span></p> <p><span style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; line-height: 18px; background-color: #fafafa;"><span style="color: #ff0000;"><strong><span class="keyword" style="color: #7f0055; font-weight: bold;">synchronized</span><span style="color: black;"> (mutex)  为什么要用mutex这个参数?</span></strong></span></span></p> <p> </p> <p>我认为<span style="color: #7f0055; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-weight: bold; line-height: 18px; background-color: #fafafa;">synchronized的参数是表明同步的是哪块,所以我</span>改成  我自己servlet中的req,也可以正常跑</p> <p> </p> <p>public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException</p> <p>       {</p> <p> </p> <p>  <span style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; line-height: 18px; background-color: #fafafa;"><span class="keyword" style="color: #7f0055; font-weight: bold;">synchronized</span><span style="color: black;"> </span><span style="color: black;">(req) </span></span></p> <p> </p> <p> </p> <p><span style="color: #ff0000;"><strong>另外 ,最关键的:Thread.sleep((long) Math.random() * 1000);</strong></span></p> <p><span style="color: #ff0000;"><strong>我改成Thread.sleep(1000);,输出的结果就好像是线程共享了??</strong></span></p> <p> </p> <p> </p> <p> </p> <p>SimpleServlet@64883c ==&gt; Thread[http-8080-3,5,main]: <br>Counter = 4<br>Counter = 6<br>Counter = 10<br>Counter = 14<br>Counter = 18<br>Counter = 22<br>Counter = 26<br>Counter = 30<br>Counter = 34<br>Counter = 38</p> <p> </p> <p> </p> <p>SimpleServlet@64883c ==&gt; Thread[http-8080-6,5,main]: <br>Counter = 4<br>Counter = 7<br>Counter = 11<br>Counter = 15<br>Counter = 19<br>Counter = 23<br>Counter = 27<br>Counter = 31<br>Counter = 35<br>Counter = 39</p> <p> </p> <p>SimpleServlet@64883c ==&gt; Thread[http-8080-7,5,main]: <br>Counter = 4<br>Counter = 8<br>Counter = 12<br>Counter = 16<br>Counter = 20<br>Counter = 24<br>Counter = 28<br>Counter = 32<br>Counter = 36<br>Counter = 40</p> <p> </p> <p> </p> <p>下面附上  ZWCHEN先生的原文,并对他表示深深的敬意,谢谢!</p> <p><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25px;"> </span></p> <div class="dp-highlighter" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; margin-left: 9px; padding: 1px;"> <div class="bar"> <div class="tools" style="text-align: left; color: black; font-weight: bold; padding: 3px; margin: 0px;">Java代码  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="14" height="15"> <param name="src"> <embed type="application/x-shockwave-flash" width="14" height="15" src="http://zwchen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf"></embed></object>  <a style="color: #108ac6; text-decoration: underline;" title="收藏这段代码" href="http://zwchen.iteye.com/blog/91088"><img class="star" style="border-color: initial;" src="http://zwchen.iteye.com/images/icon_star.png" alt="收藏代码"></a> </div> </div> <ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; background-color: #ffffff; color: #2b91af; border: 1px solid #d1d7dc;"> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">class</span><span style="color: black;"> SimpleServlet </span><span class="keyword" style="color: #7f0055; font-weight: bold;">extends</span><span style="color: black;"> HttpServlet  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">{  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       <span class="comment" style="color: #008200;">// A variable that is NOT thread-safe!</span><span style="color: black;">  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       <span class="keyword" style="color: #7f0055; font-weight: bold;">private</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">int</span><span style="color: black;"> counter = </span><span class="number" style="color: #c00000;">0</span><span style="color: black;">;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       <span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">void</span><span style="color: black;"> doGet(HttpServletRequest req, HttpServletResponse resp) </span><span class="keyword" style="color: #7f0055; font-weight: bold;">throws</span><span style="color: black;"> ServletException, IOException  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              doPost(req, resp);  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       <span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">void</span><span style="color: black;"> doPost(HttpServletRequest req, HttpServletResponse resp) </span><span class="keyword" style="color: #7f0055; font-weight: bold;">throws</span><span style="color: black;"> ServletException, IOException  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              resp.getWriter().println(<span class="string" style="color: blue;">"&lt;HTML&gt;&lt;BODY&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              resp.getWriter().println(<span class="keyword" style="color: #7f0055; font-weight: bold;">this</span><span style="color: black;"> + </span><span class="string" style="color: blue;">" ==&gt; "</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              resp.getWriter().println(Thread.currentThread() + <span class="string" style="color: blue;">": &lt;br&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              <span class="keyword" style="color: #7f0055; font-weight: bold;">for</span><span style="color: black;"> (</span><span class="keyword" style="color: #7f0055; font-weight: bold;">int</span><span style="color: black;"> c = </span><span class="number" style="color: #c00000;">0</span><span style="color: black;">; c &lt; </span><span class="number" style="color: #c00000;">10</span><span style="color: black;">; c++)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     resp.getWriter().println(<span class="string" style="color: blue;">"Counter = "</span><span style="color: black;"> + counter + </span><span class="string" style="color: blue;">"&lt;BR&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     <span class="keyword" style="color: #7f0055; font-weight: bold;">try</span><span style="color: black;">  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                            Thread.sleep((<span class="keyword" style="color: #7f0055; font-weight: bold;">long</span><span style="color: black;">) Math.random() * </span><span class="number" style="color: #c00000;">1000</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                            counter++;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     <span class="keyword" style="color: #7f0055; font-weight: bold;">catch</span><span style="color: black;"> (InterruptedException exc)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">                     }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">              resp.getWriter().println(<span class="string" style="color: blue;">"&lt;/BODY&gt;&lt;/HTML&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">       }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">}  </span></li> </ol> </div> <p> <br><br>然后,我们通过一个html页面向该servlet发出三次请求: <br></p> <div class="dp-highlighter" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; margin-left: 9px; padding: 1px;"> <div class="bar"> <div class="tools" style="text-align: left; color: black; font-weight: bold; padding: 3px; margin: 0px;">Java代码  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="14" height="15"> <param name="src"> <embed type="application/x-shockwave-flash" width="14" height="15" src="http://zwchen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf"></embed></object>  <a style="color: #108ac6; text-decoration: underline;" title="收藏这段代码" href="http://zwchen.iteye.com/blog/91088"><img class="star" style="border-color: initial;" src="http://zwchen.iteye.com/images/icon_star.png" alt="收藏代码"></a> </div> </div> <ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; background-color: #ffffff; color: #2b91af; border: 1px solid #d1d7dc;"> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;"><span style="color: black;">&lt;HTML&gt;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">&lt;BODY&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">&lt;TABLE&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        &lt;TD&gt;&lt;IFRAME src=<span class="string" style="color: blue;">"./SimpleServlet"</span><span style="color: black;"> name=</span><span class="string" style="color: blue;">"servlet1"</span><span style="color: black;"> height=</span><span class="string" style="color: blue;">"200%"</span><span style="color: black;">&gt; &lt;/IFRAME&gt;&lt;/TD&gt;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;/TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        &lt;TD&gt;&lt;IFRAME src=<span class="string" style="color: blue;">"./SimpleServlet"</span><span style="color: black;"> name=</span><span class="string" style="color: blue;">"servlet2"</span><span style="color: black;"> height=</span><span class="string" style="color: blue;">"200%"</span><span style="color: black;">&gt; &lt;/IFRAME&gt;&lt;/TD&gt;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;/TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        &lt;TD&gt;&lt;IFRAME src=<span class="string" style="color: blue;">"./SimpleServlet"</span><span style="color: black;"> name=</span><span class="string" style="color: blue;">"servlet3"</span><span style="color: black;"> height=</span><span class="string" style="color: blue;">"200%"</span><span style="color: black;">&gt; &lt;/IFRAME&gt;&lt;/TD&gt;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    &lt;/TR&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">&lt;/TABLE&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">&lt;/BODY&gt;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">&lt;/HTML&gt;  </span></li> </ol> </div> <p> <br><br><br><br>刷新页面几次后,产生的结果为: <br><br>com.zwchen.servlet.SimpleServlet@11e1bbf ==&gt; Thread[http-8081-Processor23,5,main]: <br>Counter = 60 <br>Counter = 61 <br>Counter = 62 <br>Counter = 65 <br>Counter = 68 <br>Counter = 71 <br>Counter = 74 <br>Counter = 77 <br>Counter = 80 <br>Counter = 83 <br><br><br><br>com.zwchen.servlet.SimpleServlet@11e1bbf ==&gt; Thread[http-8081-Processor22,5,main]: <br>Counter = 61 <br>Counter = 63 <br>Counter = 66 <br>Counter = 69 <br>Counter = 72 <br>Counter = 75 <br>Counter = 78 <br>Counter = 81 <br>Counter = 84 <br>Counter = 87 <br><br><br><br>com.zwchen.servlet.SimpleServlet@11e1bbf ==&gt; Thread[http-8081-Processor24,5,main]: <br>Counter = 61 <br>Counter = 64 <br>Counter = 67 <br>Counter = 70 <br>Counter = 73 <br>Counter = 76 <br>Counter = 79 <br>Counter = 82 <br>Counter = 85 <br>Counter = 88 <br><br>我们会发现三点: <br><br>servlet只产生了一个Servlet对象,因为输出this时,其hashcode都一样, <br><br>servlet在不同的线程(线程池)中运行,如http-8081-Processor22,http-8081-Processor23 <br><br>Count被这三个doGet方法共享,并且并行修改。 <br><br><br><br>上面的结果,违反了线程安全的两个方面。 <br><br>那么,我们怎样保证按照我们期望的结果运行呢?首先,我想保证产生的count都是顺序执行的。 <br><br>我们将Servlet代码重构如下: <br><br><br></p> <div class="dp-highlighter" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; margin-left: 9px; padding: 1px;"> <div class="bar"> <div class="tools" style="text-align: left; color: black; font-weight: bold; padding: 3px; margin: 0px;">Java代码  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="14" height="15"> <param name="src"> <embed type="application/x-shockwave-flash" width="14" height="15" src="http://zwchen.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf"></embed></object>  <a style="color: #108ac6; text-decoration: underline;" title="收藏这段代码" href="http://zwchen.iteye.com/blog/91088"><img class="star" style="border-color: initial;" src="http://zwchen.iteye.com/images/icon_star.png" alt="收藏代码"></a> </div> </div> <ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; background-color: #ffffff; color: #2b91af; border: 1px solid #d1d7dc;"> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">class</span><span style="color: black;"> SimpleServlet </span><span class="keyword" style="color: #7f0055; font-weight: bold;">extends</span><span style="color: black;"> HttpServlet  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">{  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  <span class="comment" style="color: #008200;">//A variable that is NOT thread-safe!</span><span style="color: black;">  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  <span class="keyword" style="color: #7f0055; font-weight: bold;">private</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">int</span><span style="color: black;"> counter = </span><span class="number" style="color: #c00000;">0</span><span style="color: black;">;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  <span class="keyword" style="color: #7f0055; font-weight: bold;">private</span><span style="color: black;"> String mutex = </span><span class="string" style="color: blue;">""</span><span style="color: black;">;  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">   </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  <span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">void</span><span style="color: black;"> doGet(HttpServletRequest req, HttpServletResponse resp)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: #7f0055; font-weight: bold;">throws</span><span style="color: black;"> ServletException, IOException  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">{  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    doPost(req, resp);  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  <span class="keyword" style="color: #7f0055; font-weight: bold;">public</span><span style="color: black;"> </span><span class="keyword" style="color: #7f0055; font-weight: bold;">void</span><span style="color: black;"> doPost(HttpServletRequest req, HttpServletResponse resp)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: #7f0055; font-weight: bold;">throws</span><span style="color: black;"> ServletException, IOException  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    resp.getWriter().println(<span class="string" style="color: blue;">"&lt;HTML&gt;&lt;BODY&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    resp.getWriter().println(<span class="keyword" style="color: #7f0055; font-weight: bold;">this</span><span style="color: black;"> + </span><span class="string" style="color: blue;">": &lt;br&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: #7f0055; font-weight: bold;">synchronized</span><span style="color: black;"> (mutex)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    {      </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">      <span class="keyword" style="color: #7f0055; font-weight: bold;">for</span><span style="color: black;"> (</span><span class="keyword" style="color: #7f0055; font-weight: bold;">int</span><span style="color: black;"> c = </span><span class="number" style="color: #c00000;">0</span><span style="color: black;">; c &lt; </span><span class="number" style="color: #c00000;">10</span><span style="color: black;">; c++)  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">      {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        resp.getWriter().println(<span class="string" style="color: blue;">"Counter = "</span><span style="color: black;"> + counter + </span><span class="string" style="color: blue;">"&lt;BR&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: #7f0055; font-weight: bold;">try</span><span style="color: black;">  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">          {  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">          Thread.sleep((<span class="keyword" style="color: #7f0055; font-weight: bold;">long</span><span style="color: black;">) Math.random() * </span><span class="number" style="color: #c00000;">1000</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">          counter++;  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: #7f0055; font-weight: bold;">catch</span><span style="color: black;"> (InterruptedException exc) { }  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">      }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">    resp.getWriter().println(<span class="string" style="color: blue;">"&lt;/BODY&gt;&lt;/HTML&gt;"</span><span style="color: black;">);  </span></span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  }  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">  </span></li> <li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: black;">}  </span></li> </ol> </div> <p> <br><br><br>我们的输出结果为: <br><br>com.zwchen.servlet.SimpleServlet@109da93: <br>Counter = 0 <br>Counter = 1 <br>Counter = 2 <br>Counter = 3 <br>Counter = 4 <br>Counter = 5 <br>Counter = 6 <br>Counter = 7 <br>Counter = 8 <br>Counter = 9 <br><br><br>com.zwchen.servlet.SimpleServlet@109da93: <br>Counter = 10 <br>Counter = 11 <br>Counter = 12 <br>Counter = 13 <br>Counter = 14 <br>Counter = 15 <br>Counter = 16 <br>Counter = 17 <br>Counter = 18 <br>Counter = 19 <br><br>com.zwchen.servlet.SimpleServlet@109da93: <br>Counter = 20 <br>Counter = 21 <br>Counter = 22 <br>Counter = 23 <br>Counter = 24 <br>Counter = 25 <br>Counter = 26 <br>Counter = 27 <br>Counter = 28 <br>Counter = 29 <br><br><br>这符合了我们的要求,输出都是按顺序的,这正式synchronized的含义。 <br><br>附带说一下,我现在synchronized的是一个字符串变量mutex,不是this对象,这主要是从performance和Scalability考虑。Synchronized用在this对象上,会带来严重的可伸缩性的问题(Scalability),所有的并发请求都要排队!</p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p>

请大虾来看看lucene高亮问题

package com.pohsin.test; import java.io.IOException; import java.io.StringReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; public class MyHighLighter { private String indexPath = "F:\\index"; private Analyzer analyzer; private IndexSearcher searcher; public MyHighLighter(){ analyzer = new StandardAnalyzer(); } public void createIndex() throws IOException { // 该方法建立索引 IndexWriter writer = new IndexWriter(indexPath,analyzer,true); Document docA = new Document(); String fileTextA = "因为火烧云总是燃烧着消失在太阳冲下地平线的时刻,然后便是宁静的自然的天籁,没有谁会在这样的时光的镜片里伤感自语,因为灿烂给人以安静的舒适感。"; Field fieldA = new Field("contents", fileTextA, Field.Store.YES,Field.Index.TOKENIZED); docA.add(fieldA); Document docB = new Document(); String fileTextB = "因为带有以伤痕为代价的美丽风景总是让人不由地惴惴不安,紧接着袭面而来的抑或是病痛抑或是灾难,没有谁会能够安逸着恬然,因为模糊让人撕心裂肺地想呐喊。"; Field fieldB = new Field("contents", fileTextB, Field.Store.YES,Field.Index.TOKENIZED); docB.add(fieldB); Document docC = new Document(); String fileTextC = "我喜欢上了一个人孤独地行游,在梦与海洋的交接地带炽烈燃烧着。"+ "因为,一条孤独的鱼喜欢上了火焰的颜色,真是荒唐地不合逻辑。"; Field fieldC = new Field("contents", fileTextC, Field.Store.YES,Field.Index.TOKENIZED); docC.add(fieldC); writer.addDocument(docA); writer.addDocument(docB); writer.addDocument(docC); writer.optimize(); writer.close(); } public void search(String fieldName,String keyword) throws IOException, ParseException{ // 检索的方法,并实现高亮显示 searcher = new IndexSearcher(indexPath); QueryParser queryParse = new QueryParser(fieldName, analyzer); // 构造QueryParser,解析用户输入的检索关键字 Query query = queryParse.parse(keyword); Hits hits = searcher.search(query); for(int i=0;i<hits.length();i++){ Document doc = hits.doc(i); String text = doc.get(fieldName); SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>", "</font>"); Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(text.length())); if (text != null) { TokenStream tokenStream = analyzer.tokenStream(fieldName,new StringReader(text)); String highLightText = highlighter.getBestFragment(tokenStream, text); System.out.println("★高亮显示第 "+(i+1) +" 条检索结果如下所示:"); System.out.println(highLightText); } } searcher.close(); } public static void main(String[] args) { // 测试主函数 MyHighLighter mhl = new MyHighLighter(); try { mhl.createIndex(); mhl.search("contents", "因为"); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } } 这是测试文件,但是在我运行的时候报错,请大虾们帮忙看一下 Exception in thread "main" java.lang.NoSuchMethodError: org.apache.lucene.analysis.Token: method <init>()V not found at org.apache.lucene.search.highlight.Highlighter.getBestTextFragments(Highlighter.java:220) at org.apache.lucene.search.highlight.Highlighter.getBestFragments(Highlighter.java:177) at org.apache.lucene.search.highlight.Highlighter.getBestFragment(Highlighter.java:103) at com.pohsin.test.MyHighLighter.search(MyHighLighter.java:69) at com.pohsin.test.MyHighLighter.main(MyHighLighter.java:82)

HighlightJS在WordPress中没有突出显示

<div class="post-text" itemprop="text"> <p>Unfortunately, HighlightJS not highlighting my code in WordPress site. The highlightJS background shows, but the syntax isn't highlighting.</p> <p>Here's the following code:</p> <pre><code>class WordPress_Code_Highlighter { public function __construct() { add_action( 'wp_enqueue_scripts', [ $this, 'register_styles' ], 15 ); add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] ); } public function register_styles() { wp_register_style( 'wordpress-code-highlighter', plugin_dir_url( __FILE__ ) . 'bower_components/highlight.js/src/styles/dracula.css', [ ], '9.3.0' ); wp_enqueue_style( 'wordpress-code-highlighter' ); } public function register_scripts() { wp_register_script( 'wordpress-code-highlight', plugin_dir_url( __FILE__ ) . 'bower_components/highlight.js/src/highlight.js', [ ], '9.3.0', true ); wp_enqueue_script( 'wordpress-code-highlight' ); wp_register_script( 'wordpress-code-highlighter', plugin_dir_url( __FILE__ ) . 'resources/assets/js/wordpress-code-highlighter.js', [ ], '1.0', true ); wp_enqueue_script( 'wordpress-code-highlighter' ); } } new WordPress_Code_Highlighter(); </code></pre> <p>Here's the Javascript.</p> <pre><code>jQuery(document).ready(function () { jQuery('pre code').each(function (i, block) { hljs.highlightBlock(block); }); }); </code></pre> <p>For your convenience, I also attached a screenshot. <a href="https://i.stack.imgur.com/zOglb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zOglb.png" alt="HighlightJS Syntax Not Highlighting"></a></p> </div>

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

Java校招入职华为,半年后我跑路了

何来 我,一个双非本科弟弟,有幸在 19 届的秋招中得到前东家华为(以下简称 hw)的赏识,当时秋招签订就业协议,说是入了某 java bg,之后一系列组织架构调整原因等等让人无法理解的神操作,最终毕业前夕,被通知调往其他 bg 做嵌入式开发(纯 C 语言)。 由于已至于校招末尾,之前拿到的其他 offer 又无法再收回,一时感到无力回天,只得默默接受。 毕业后,直接入职开始了嵌入式苦旅,由于从未...

Java基础知识面试题(2020最新版)

文章目录Java概述何为编程什么是Javajdk1.5之后的三大版本JVM、JRE和JDK的关系什么是跨平台性?原理是什么Java语言有哪些特点什么是字节码?采用字节码的最大好处是什么什么是Java程序的主类?应用程序和小程序的主类有何不同?Java应用程序与小程序之间有那些差别?Java和C++的区别Oracle JDK 和 OpenJDK 的对比基础语法数据类型Java有哪些数据类型switc...

@程序员:GitHub这个项目快薅羊毛

今天下午在朋友圈看到很多人都在发github的羊毛,一时没明白是怎么回事。 后来上百度搜索了一下,原来真有这回事,毕竟资源主义的羊毛不少啊,1000刀刷爆了朋友圈!不知道你们的朋友圈有没有看到类似的消息。 这到底是啥情况? 微软开发者平台GitHub 的一个区块链项目 Handshake ,搞了一个招募新会员的活动,面向GitHub 上前 25万名开发者派送 4,246.99 HNS币,大约价...

再不跳槽,应届毕业生拿的都比我多了!

跳槽几乎是每个人职业生涯的一部分,很多HR说“三年两跳”已经是一个跳槽频繁与否的阈值了,可为什么市面上有很多程序员不到一年就跳槽呢?他们不担心影响履历吗? PayScale之前发布的**《员工最短任期公司排行榜》中,两家码农大厂Amazon和Google**,以1年和1.1年的员工任期中位数分列第二、第四名。 PayScale:员工最短任期公司排行榜 意外的是,任期中位数极小的这两家公司,薪资...

我以为我学懂了数据结构,直到看了这个导图才发现,我错了

数据结构与算法思维导图

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

华为初面+综合面试(Java技术面)附上面试题

华为面试整体流程大致分为笔试,性格测试,面试,综合面试,回学校等结果。笔试来说,华为的难度较中等,选择题难度和网易腾讯差不多。最后的代码题,相比下来就简单很多,一共3道题目,前2题很容易就AC,题目已经记不太清楚,不过难度确实不大。最后一题最后提交的代码过了75%的样例,一直没有发现剩下的25%可能存在什么坑。 笔试部分太久远,我就不怎么回忆了。直接将面试。 面试 如果说腾讯的面试是挥金如土...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

讲一个程序员如何副业月赚三万的真实故事

loonggg读完需要3分钟速读仅需 1 分钟大家好,我是你们的校长。我之前讲过,这年头,只要肯动脑,肯行动,程序员凭借自己的技术,赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

win10暴力查看wifi密码

刚才邻居打了个电话说:喂小灰,你家wifi的密码是多少,我怎么连不上了。 我。。。 我也忘了哎,就找到了一个好办法,分享给大家: 第一种情况:已经连接上的wifi,怎么知道密码? 打开:控制面板\网络和 Internet\网络连接 然后右击wifi连接的无线网卡,选择状态 然后像下图一样: 第二种情况:前提是我不知道啊,但是我以前知道密码。 此时可以利用dos命令了 1、利用netsh wlan...

上班一个月,后悔当初着急入职的选择了

最近有个老铁,告诉我说,上班一个月,后悔当初着急入职现在公司了。他之前在美图做手机研发,今年美图那边今年也有一波组织优化调整,他是其中一个,在协商离职后,当时捉急找工作上班,因为有房贷供着,不能没有收入来源。所以匆忙选了一家公司,实际上是一个大型外包公司,主要派遣给其他手机厂商做外包项目。**当时承诺待遇还不错,所以就立马入职去上班了。但是后面入职后,发现薪酬待遇这块并不是HR所说那样,那个HR自...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

总结了 150 余个神奇网站,你不来瞅瞅吗?

原博客再更新,可能就没了,之后将持续更新本篇博客。

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

程序员写出这样的代码,能不挨骂吗?

当你换槽填坑时,面对一个新的环境。能够快速熟练,上手实现业务需求是关键。但是,哪些因素会影响你快速上手呢?是原有代码写的不够好?还是注释写的不够好?昨夜...

!大部分程序员只会写3年代码

如果世界上都是这种不思进取的软件公司,那别说大部分程序员只会写 3 年代码,恐怕就没有程序员这种职业。

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

HTTP与HTTPS的区别

面试官问HTTP与HTTPS的区别,我这样回答让他竖起大拇指!

男生更看重女生的身材脸蛋,还是思想?

往往,我们看不进去大段大段的逻辑。深刻的哲理,往往短而精悍,一阵见血。问:产品经理挺漂亮的,有点心动,但不知道合不合得来。男生更看重女生的身材脸蛋,还是...

程序员为什么千万不要瞎努力?

本文作者用对比非常鲜明的两个开发团队的故事,讲解了敏捷开发之道 —— 如果你的团队缺乏统一标准的环境,那么即使勤劳努力,不仅会极其耗时而且成果甚微,使用...

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试:第十六章:Java中级开发(16k)

HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

终于懂了TCP和UDP协议区别

终于懂了TCP和UDP协议区别

Python爬虫,高清美图我全都要(彼岸桌面壁纸)

爬取彼岸桌面网站较为简单,用到了requests、lxml、Beautiful Soup4

立即提问
相关内容推荐