s = 'ABCDEF'
#索引
#s1,s2,s3...都是新字符串与s无关了
s1 = s[0]
s2 = s[-1] #最后一个元素
s3 = s[0:4] #左闭右开
print(s1) #A
print(s2) #F
print(s3) #ABC
#打印全部
s4 = s[:] #s[0:]
print(s4)
s5 = s[0:5:2] #[首:位:步长] 步长为正,正着取,步长为负,倒着取
print(s5) #ACE
s6 = s[3::-1]
print(s6) #DCBA
#倒置
s7 = s[::-1]
print(s7) #FEDCBA
/**
*Executor:执行器,只有一个execute方法。
*
* @author zhouyi
*
*/
public class MyExecutor implements Executor {
public static void main(String[] args){
new MyExecutor().execute(()->System.out.println("hello!zhouyi"));
}
@Override
public void execute(Runnable command) {
command.run();
}
}
public class myThreadPool {
public static void main(String[] args) throws InterruptedException {
// 固定线程池 ,ExecutorService可以往里扔任务的,execute,submit
ExecutorService service = Executors.newFixedThreadPool(5); // 启5个线程池
for(int i = 0; i < 6; i++) { // 扔6个任务进去
service.execute(()->{
try {
TimeUnit.MICROSECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
}
System.out.println(service);
service.shutdown();
System.out.println(service.isTerminated()); // 有没有执行完了
System.out.println(service.isShutdown()); // 是否关了
System.out.println(service);
TimeUnit.SECONDS.sleep(5);
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
System.out.println(service);
}
}
public class MyparallelComputing {
public static void main(String[] args) throws InterruptedException,ExecutionException{
long start = System.currentTimeMillis();
// 只用一个主线程
List<Integer> list = getPrime(1,200000);
long end = System.currentTimeMillis();
System.out.println(end - start);
// 利用线程池去实现
final int cpuCoreNum = 4;
ExecutorService service = Executors.newFixedThreadPool(cpuCoreNum);
MyTask task1 = new MyTask(1, 80000);
MyTask task2 = new MyTask(80001, 130000);
MyTask task3 = new MyTask(130001,170000);
MyTask task4 = new MyTask(170001, 200000);
Future<List<Integer>> future1 = service.submit(task1);
Future<List<Integer>> future2 = service.submit(task2);
Future<List<Integer>> future3 = service.submit(task3);
Future<List<Integer>> future4 = service.submit(task4);
start = System.currentTimeMillis();
future1.get(); // 阻塞
future2.get();
future3.get();
future4.get();
end = System.currentTimeMillis();
System.out.println(end - start);
}
static class MyTask implements Callable<List<Integer>> {
int startPos,endPos;
MyTask(int s, int e) {
this.startPos = s;
this.endPos = e;
}
@Override
public List<Integer> call() throws Exception {
List<Integer> list = getPrime(startPos, endPos);
return list;
}
}
static boolean isPrime(int a) {
for(int i = 1; i < a/2; i++) {
if(a % i == 0) return false;
}
return true;
}
static List<Integer> getPrime(int start, int end) {
List<Integer> list = new ArrayList<Integer>();
for(int i =start; i < end; i++) {
if (isPrime(i)) list.add(i);
}
return list;
}
}
public class MynewWorkStealingPool {
public static void main(String[] args) throws IOException{
ExecutorService service = Executors.newWorkStealingPool();
// 判断cpu是几核的,得到结果4核
System.out.println(Runtime.getRuntime().availableProcessors());
service.execute(new R(1000)); // daemon 经营线程
service.execute(new R(2000));
service.execute(new R(3000));
service.execute(new R(5000));
service.execute(new R(7000));
// 这句话不要是看不到结果的,原因是因为这个线程池的特殊
System.in.read();
}
static class R implements Runnable {
int time;
R(int t) {
this.time = t;
}
@Override
public void run() {
try {
TimeUnit.MICROSECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
}例:将文本串中将李白的《静夜思》的各个部分分别提取出来,并格式化输出。标题加粗,文本居中对齐,诗歌正文颜色显示灰色
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript">
var comment = "静夜思李白床前明月光,疑是地上霜。举头望明月,低头思故乡。";
var partial = comment.substring(0,3); //取出标题
partial = partial.bold(); //标题加粗
document.write("<p align=\"center\">");//输出HTML标签"<p>",并设置居中对齐
docunment.write(partial); //输出标题
partial = comment.slice(3,5); //取出作者
document.write("<br />"); //输出换行标签<br>
document.write(partial); //输出作者
partial = comment.slice(5,17); //取出第一句诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
partial = comment.slice(17,29); //取出第二就诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
document.write("</p>");
</script>
</body>
</html>using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
abstract class PersonBuilder
{
protected Graphics g;
protected Pen p;
public PersonBuilder(Graphics g, Pen p)
{
this.g = g;
this.p = p;
}
public abstract void BuildHead();
public abstract void BuildArmLeft();
public abstract void BuildArmRight();
public abstract void BuildBody();
public abstract void BuildLegLeft();
public abstract void BuildLegRight();
}
}using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
class ThinnerBuilder : PersonBuilder
{
public ThinnerBuilder(Graphics g,Pen p) : base(g, p) { }
public override void BuildArmLeft()
{
g.DrawLine(p, 60, 50, 40, 100);
}
public override void BuildArmRight()
{
g.DrawLine(p, 70, 50, 90, 100);
}
public override void BuildBody()
{
g.DrawRectangle(p, 60, 50, 10, 50);
}
public override void BuildHead()
{
g.DrawEllipse(p, 50, 20, 30, 30);
}
public override void BuildLegLeft()
{
g.DrawLine(p, 60, 100, 45, 150);
}
public override void BuildLegRight()
{
g.DrawLine(p, 70, 100, 85, 150);
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
class FatterBuilder:PersonBuilder
{
public FatterBuilder(Graphics g, Pen p) : base(g, p) { }
public override void BuildArmLeft()
{
g.DrawLine(p, 60, 50, 40, 100);
}
public override void BuildArmRight()
{
g.DrawLine(p, 70, 50, 90, 100);
}
public override void BuildBody()
{
g.DrawRectangle(p, 90, 70, 40, 70);
}
public override void BuildHead()
{
g.DrawEllipse(p, 50, 20, 30, 30);
}
public override void BuildLegLeft()
{
g.DrawLine(p, 60, 100, 45, 150);
}
public override void BuildLegRight()
{
g.DrawLine(p, 70, 100, 85, 150);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 建造者模式1
{
public partial class Form1 : Form
{
public Form1()
{
// Graphics pictureBox= CreateGraphics();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Pen p = new Pen(Color.Black);
ThinnerBuilder pb = new ThinnerBuilder(pictureBox2.CreateGraphics(), p);
PersonManage pp = new PersonManage(pb);
pp.CreatePerson();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void pictureBox3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Pen p = new Pen(Color.Black);
FatterBuilder pb = new FatterBuilder(pictureBox3.CreateGraphics(), p);
PersonManage pp = new PersonManage(pb);
pp.CreatePerson();
}
}
}