-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.huifer.concurrence.ch1; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* <p>Title : ThreadYield </p> | ||
* <p>Description : yield</p> | ||
* | ||
* @author huifer | ||
* @date 2019-03-27 | ||
*/ | ||
public class ThreadYield { | ||
|
||
public static void main(String[] args) { | ||
Task task1 = new Task(true); | ||
Task task2 = new Task(false); | ||
new Thread(task1).start(); | ||
new Thread(task2).start(); | ||
} | ||
|
||
|
||
private static class Task implements Runnable { | ||
|
||
private final boolean isYield; | ||
private List<String> stringList = new ArrayList<>(); | ||
|
||
public Task(boolean isYield) { | ||
this.isYield = isYield; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
String name = Thread.currentThread().getName(); | ||
System.out.println(name + " start:"); | ||
for (int i = 0; i < 1000000; i++) { | ||
if (isYield) { | ||
stringList.add("NO." + i); | ||
Thread.yield(); | ||
} | ||
} | ||
System.out.println(name + " end:"); | ||
|
||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.huifer.security.validate.code.impl; | ||
|
||
import com.huifer.security.properties.SecurityProperties; | ||
import com.huifer.security.validate.code.ImageCode; | ||
import com.huifer.security.validate.code.ValidateCodeGenerator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.social.connect.web.HttpSessionSessionStrategy; | ||
import org.springframework.social.connect.web.SessionStrategy; | ||
import org.springframework.web.bind.ServletRequestUtils; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
import java.util.Random; | ||
|
||
/** | ||
* 描述: | ||
* | ||
* @author: huifer | ||
* @date: 2019-11-17 | ||
*/ | ||
public class ImageCodeGenerator implements ValidateCodeGenerator { | ||
SecurityProperties securityProperties; | ||
|
||
private Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
@Override | ||
public ImageCode generate(HttpServletRequest request) { | ||
int width = ServletRequestUtils.getIntParameter(request, "width", securityProperties.getCode().getImage().getWidth()); | ||
int height = ServletRequestUtils.getIntParameter(request, "height", securityProperties.getCode().getImage().getHeight()); | ||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||
|
||
Graphics g = image.getGraphics(); | ||
|
||
Random random = new Random(); | ||
|
||
g.setColor(getRandColor(200, 250)); | ||
g.fillRect(0, 0, width, height); | ||
g.setFont(new Font("Times New Roman", Font.ITALIC, 20)); | ||
g.setColor(getRandColor(160, 200)); | ||
for (int i = 0; i < 155; i++) { | ||
int x = random.nextInt(width); | ||
int y = random.nextInt(height); | ||
int xl = random.nextInt(12); | ||
int yl = random.nextInt(12); | ||
g.drawLine(x, y, x + xl, y + yl); | ||
} | ||
|
||
StringBuilder sRand = new StringBuilder(); | ||
|
||
for (int i = 0; i < securityProperties.getCode().getImage().getLength(); i++) { | ||
String rand = String.valueOf(random.nextInt(10)); | ||
sRand.append(rand); | ||
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); | ||
g.drawString(rand, 13 * i + 6, 16); | ||
} | ||
|
||
g.dispose(); | ||
log.info("验证码={}", sRand.toString()); | ||
return new ImageCode(image, sRand.toString(), securityProperties.getCode().getImage().getExpireIn()); | ||
|
||
} | ||
|
||
private Color getRandColor(int fc, int bc) { | ||
Random random = new Random(); | ||
if (fc > 255) { | ||
fc = 255; | ||
} | ||
if (bc > 255) { | ||
bc = 255; | ||
} | ||
int r = fc + random.nextInt(bc - fc); | ||
int g = fc + random.nextInt(bc - fc); | ||
int b = fc + random.nextInt(bc - fc); | ||
return new Color(r, g, b); | ||
} | ||
|
||
|
||
public void setSecurityProperties(SecurityProperties securityProperties) { | ||
this.securityProperties = securityProperties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.cache; | ||
|
||
import org.apache.ibatis.cache.decorators.SerializedCache; | ||
import org.apache.ibatis.cache.decorators.WeakCache; | ||
import org.apache.ibatis.cache.impl.PerpetualCache; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class WeakCacheTest { | ||
|
||
@Test | ||
void shouldDemonstrateObjectsBeingCollectedAsNeeded() { | ||
final int N = 3000000; | ||
WeakCache cache = new WeakCache(new PerpetualCache("default")); | ||
for (int i = 0; i < N; i++) { | ||
cache.putObject(i, i); | ||
if (cache.getSize() < i + 1) { | ||
// System.out.println("Cache exceeded with " + (i + 1) + " entries."); | ||
break; | ||
} | ||
if ((i + 1) % 100000 == 0) { | ||
// Try performing GC. | ||
System.gc(); | ||
} | ||
} | ||
assertTrue(cache.getSize() < N); | ||
} | ||
|
||
@Test | ||
void shouldDemonstrateCopiesAreEqual() { | ||
Cache cache = new WeakCache(new PerpetualCache("default")); | ||
cache = new SerializedCache(cache); | ||
for (int i = 0; i < 1000; i++) { | ||
cache.putObject(i, i); | ||
Object value = cache.getObject(i); | ||
assertTrue(value == null || value.equals(i)); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldRemoveItemOnDemand() { | ||
WeakCache cache = new WeakCache(new PerpetualCache("default")); | ||
cache.putObject(0, 0); | ||
assertNotNull(cache.getObject(0)); | ||
cache.removeObject(0); | ||
assertNull(cache.getObject(0)); | ||
} | ||
|
||
@Test | ||
void shouldFlushAllItemsOnDemand() { | ||
WeakCache cache = new WeakCache(new PerpetualCache("default")); | ||
for (int i = 0; i < 5; i++) { | ||
cache.putObject(i, i); | ||
} | ||
assertNotNull(cache.getObject(0)); | ||
assertNotNull(cache.getObject(4)); | ||
cache.clear(); | ||
assertNull(cache.getObject(0)); | ||
assertNull(cache.getObject(4)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright 2009-2017 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.type; | ||
|
||
import java.sql.*; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author Clinton Begin | ||
*/ | ||
public class DateTypeHandler extends BaseTypeHandler<Date> { | ||
|
||
@Override | ||
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) | ||
throws SQLException { | ||
ps.setTimestamp(i, new Timestamp(parameter.getTime())); | ||
} | ||
|
||
@Override | ||
public Date getNullableResult(ResultSet rs, String columnName) | ||
throws SQLException { | ||
Timestamp sqlTimestamp = rs.getTimestamp(columnName); | ||
if (sqlTimestamp != null) { | ||
return new Date(sqlTimestamp.getTime()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Date getNullableResult(ResultSet rs, int columnIndex) | ||
throws SQLException { | ||
Timestamp sqlTimestamp = rs.getTimestamp(columnIndex); | ||
if (sqlTimestamp != null) { | ||
return new Date(sqlTimestamp.getTime()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Date getNullableResult(CallableStatement cs, int columnIndex) | ||
throws SQLException { | ||
Timestamp sqlTimestamp = cs.getTimestamp(columnIndex); | ||
if (sqlTimestamp != null) { | ||
return new Date(sqlTimestamp.getTime()); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.huifer.idgen.my.service.conv; | ||
|
||
import com.huifer.idgen.my.service.bean.Id; | ||
import com.huifer.idgen.my.service.bean.IdMeta; | ||
import com.huifer.idgen.my.service.bean.enums.IdType; | ||
import com.huifer.idgen.my.service.factory.IdMetaFactory; | ||
|
||
/** | ||
* @author: wang | ||
* @description: | ||
*/ | ||
public class IdConverterImpl implements IdConverter { | ||
|
||
private IdMeta idMeta; | ||
|
||
public IdConverterImpl(IdType idType) { | ||
this(IdMetaFactory.getIdMeta(idType)); | ||
} | ||
|
||
public IdConverterImpl(IdMeta idMeta) { | ||
this.idMeta = idMeta; | ||
} | ||
|
||
|
||
@Override | ||
public long converter(Id id) { | ||
return doConverter(id, this.idMeta); | ||
} | ||
|
||
@Override | ||
public Id converter(long id) { | ||
return doConvert(id, idMeta); | ||
} | ||
|
||
/** | ||
* 根据id生产,根据id元数据生产一个long类型的id | ||
* | ||
* @param id | ||
* @param idMeta | ||
* @return | ||
*/ | ||
protected long doConverter(Id id, IdMeta idMeta) { | ||
long res = 0; | ||
res |= id.getMachine(); | ||
res |= id.getSeq() << idMeta.getMachineBits(); | ||
res |= id.getTime() << idMeta.getTimeBitsStartPos(); | ||
res |= id.getGenMethod() << idMeta.getGenMethodBitsStartPos(); | ||
res |= id.getType() << idMeta.getTypeBitsStartPos(); | ||
res |= id.getVersion() << idMeta.getVersionBitsStartPos(); | ||
return res; | ||
} | ||
|
||
/** | ||
* 根据long类型id,根据id元数据返回这个id的意义 | ||
* | ||
* @param id | ||
* @param idMeta | ||
* @return | ||
*/ | ||
protected Id doConvert(long id, IdMeta idMeta) { | ||
Id res = new Id(); | ||
res.setMachine(id & idMeta.getMachineBitsMask()); | ||
res.setSeq((id >>> idMeta.getMachineBits()) & idMeta.getSeqBitsMask()); | ||
res.setTime((id >>> idMeta.getTimeBitsStartPos()) & idMeta.getTimeBitsMask()); | ||
res.setGenMethod((id >>> idMeta.getGenMethodBitsStartPos()) & idMeta.getGenMethodBitsMask()); | ||
res.setType((id >>> idMeta.getTypeBitsStartPos()) & idMeta.getTypeBitsMask()); | ||
res.setVersion((id >>> idMeta.getVersionBitsStartPos()) & idMeta.getVersionBitsMask()); | ||
return res; | ||
} | ||
} |